

////////////////////////////////////////

timer clear
timer on 1

// TITLE: Pesticides: What you don't know can hurt you

//   Purpose of .do file:
/*
    ANALYSIS FOR TABLES IN THE PAPER & APPENDICES

//			SECTION 1: PESTICIDE CHOICE TOXICITY ANALYSIS
Table 1. Intention-to-treat effects of information on relative toxicity knowledge and PPE knowledge scores
Table 2. Intention-to-treat effects of information on changes in choice toxicity across survey rounds from choice experiment data
Table 3. Average partial effects of information on pesticide choice probabilities and price coefficients from conditional logit estimations of choice experiment data
Table 4. Intention-to-treat effects of information on revealed pesticide demand at endline
Table 5. Revealed demand pesticide market shares for different toxicity levels by pest and by survey round
		

//			SECTION 2: PPE DEMAND
Table 6. Intention-to-treat effects of information on WTP for gloves and masks from Becker-DeGroot-Marschak mechanisms bid data
Table 7. Heterogeneous treatment effects of information on WTP for gloves and masks by baseline pesticide use from Becker-DeGroot-Marschak mechanism bid data

	
	
//			SECTION 3: APPENDICES
	APPENDIX A: SUPPORTING FIGURES AND TABLES
Table A1. Balance tests of random treatment assignment 
Table A2. PPE ownership for tomato farmers in Mkushi, Zambia at the baseline
Table A3. Effects of information on WTP for gloves and masks using BDM bid data - Linear projection and Tobit models 
Table A4. Price elasticity of demand estimates by price (N= 505)
Table A5. Endline PPE purchases by treatment assignment
Table A6. Pesticide products and prices used in choice experiment choice sets
Table A7. Intention-to-treat effects of information on changes in choice toxicity across survey rounds from choice experiment data - Alternative toxicity change codes 
Table A8. Revealed demand sub-sample balance tests of random treatment assignment (n=163)
Table A9. Stated choice toxicity market shares by treatment and control group assignment
Table A10. Chi-squared tests of differences in the average partial effects of treatment for changes in choice toxicity presented in Table 2
Table A11. Treatment on treated effects of information on WTP for gloves and masks from Becker-DeGroot-Marschak mechanisms bid data

APPENDIX B: RISK SUBSTITUTION TESTS
Table B1. Average partial effects of knowledge on WTP for gloves and masks

Figure A5: Kernel density plots of price per liter of class Ib and class U insecticides

APPENDIX C: TRIMMED SAMPLE RESULTS
Table C1. Trimmed sample comparison to Table 1. Intention-to-treat effects of information on relative toxicity knowledge and PPE knowledge scores
Table C2. Trimmed sample comparison to Table 2. Intention-to-treat effects of information on changes in choice toxicity across survey rounds from choice experiment data
Table C3. Trimmed sample comparison to Table 3. Average partial effects of information on pesticide choice probabilities and price coefficients from conditional logit estimations of choice experiment data
Table C4. Trimmed sample comparison to Table 4. Intention-to-treat effects of information on revealed pesticide demand at endline
Table C5. Trimmed sample comparison to Table 5. Revealed demand pesticide market shares for different toxicity levels by pest and by survey round


*/
clear all
set more off

*Globals
*Set working Directory - MATCH TO YOUR FILE LOCATIONS
cap cd "C:\Users\tjb0217\Box Sync\Tyler\One off tasks\Pesticides_JAERE"
	global pesticides `c(pwd)'
	global temp	    	"${pesticides}\temp"
	global Round1		"${pesticides}\Round1"
	global Round2		"${pesticides}\Round2"
	global output	    "${pesticides}\Output"


////////	***********		///////////		**********		////////////

////////	***********		///////////		**********		////////////


//////////			SECTION 1: PESTICIDE CHOICE TOXICITY ANALYSIS

			
////////	***********		///////////		**********		////////////

////////	***********		///////////		**********		////////////

////////////////////////////////////////////////////////

			// 	TABLE 1		///

////////////////////////////////////////////////////////


// 						CREATE DATA FILE & VARIABLES

////////////   ------------------  ///////////////////  -----------------//////////////////
use "${temp}\FMRcharacteristics.dta", clear
joinby FMR using "${temp}\Income_PCA.dta", unmatched(both) _merge(_2)
joinby FMR using "${temp}\Rd2_KnowledgeScore_11.1.16.dta", unmatched(both) _merge(_7)
joinby FMR using "${Round2}\ScenarioPerc_Decision.dta", unmatched(both) _merge(_8)
joinby FMR using "${temp}\Rd1_SourcesInfo.dta", unmatched(both) _merge(_9)
drop _*
 
// create dummy variable for receiving either type of treatment
gen _treated = (training==1|letter==1)
// create complier dummy variable
gen complier = (training==1&letter==1)

// Create dummy variable for primary education (completed grade 7)
gen primary = 1 if ed>7 & ed!=.
replace primary = 0 if primary==. & ed!=.


// create PPE knowledge variable
gen k_ppe = (K2_gloves + K2_mask)

//// create a single variable for in sample
gen insample = (replace!=1)

/// INCLUDE ALL TOX INFORMATION
// tox knowledge is about differentiating expected health by toxicity, so we can use our two toxicity identification questions
gen k_U = DEC_07==4|DEC_07==5  // say class U has low health risk
gen k_Ib = DEC_06==1|DEC_06==2  // say class Ib has high health risk
gen kIbU = k_U * k_Ib

///////////////////////////////

// Table 1 Output 
foreach outcome in kIbU k_ppe {
	// Regression 1
	
	// random inference tests
	ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(321) :  reg `outcome' TREAT i.block if insample==1, vce(cluster EA)
	matrix `outcome'_rit = r(p)
	local ritest_p = `outcome'_rit[1,1]

	// means
	reg `outcome' i.TREAT if insample == 1 & TREAT == 0
	matrix `outcome'_mean = e(b)
	local means_all = `outcome'_mean[1,2]
	
	// econometric output
	reg `outcome' TREAT i.block if insample==1, vce(cluster EA)
	testparm TREAT
	local pvalue_all=r(p)
	boottest TREAT, nograph
	local pvalue_wcb = r(p)
	
	eststo table1_`outcome'_reg1, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')
	
	// Regression 2
	
	// random inference tests
	ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(321) :  reg `outcome' TREAT Bus read HORT_all i.block if insample==1, vce(cluster EA)
	matrix `outcome'_rit = r(p)
	local ritest_p = `outcome'_rit[1,1]

	// means
	reg `outcome' i.TREAT if insample == 1 & TREAT == 0
	matrix `outcome'_mean = e(b)
	local means_all = `outcome'_mean[1,2]
	
	// econometric output
	reg `outcome' TREAT Bus read HORT_all i.block if insample==1, vce(cluster EA)
	testparm TREAT
	local pvalue_all=r(p)
	boottest TREAT, nograph
	local pvalue_wcb = r(p)
	
	eststo table1_`outcome'_reg2, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')
	
}

// Exporting the regression results
xml_tab table1_* , save($output/Table_1.xls) sd2 replace stats(N r2 pvalue_all pvalue_wcb ritest_p means_all) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(Table 1 Regression Results) 


// save file for robustness check later
save "${temp}/_tableOne.dta", replace


////////////////////////////////////////////////////////

			// 	TABLE 2		///

////////////////////////////////////////////////////////

//////  Nematode Choices  ////////
use "${temp}\Rd1_NemChoices_tox.dta", clear
joinby FMR Scenario_Number using "${temp}\Rd2_NemChoices_tox.dta", unmatched(both) _merge(_1)
joinby FMR using "${temp}\FMRcharacteristics.dta", unmatched(both) _merge(_3)

tab _1 // unmatched observations had wrong block in either round


/// add blocking variables if for difference regression
rename Block block1
joinby EA using "${Round1}\treatment.dta", unmatched(both) _merge(_2)
drop _2 _3

gen wrong_block2 = 0
replace wrong_block2 = 1 if FMR ==1397|FMR==1414|FMR==2057|FMR==2099|FMR==2116|FMR==3006|FMR==3042

// create variable for price differences across rounds
gen wrong_price = 0
replace wrong_price =1 if design==1 & Scenario_Number==6
replace wrong_price =1 if design==1 & Scenario_Number==13
replace wrong_price =1 if design==2 & Scenario_Number==6

/// expand to facilitate DiD regressions
expand 2
by FMR Scenario_Number, sort : egen float _2 = seq(), from(1) to(10) block(1)

gen ToxScore = Rd1_Tox if _2==1
replace ToxScore = Rd2_Tox if _2==2
replace ToxScore=. if Rd1_Tox==.|Rd2_Tox==.

rename _2 rd
replace rd = 0 if rd==1
replace rd = 1 if rd==2
drop if EA==.

gen change_tox = (Rd2_Tox - Rd1_Tox)
sum Rd1_Tox if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1



			/////////   TABLE 2  NEMATODES ////////////
			
// Table 2 Output 
// Regression 1 - LPM

// random inference tests
ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(321) :  reg change_tox TREAT i.block if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
matrix LPM_rit = r(p)
local ritest_p = LPM_rit[1,1]

// means
sum Rd1_Tox if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1
local means_all = r(mean)

// econometric output
reg change_tox TREAT i.block if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
testparm TREAT
local pvalue_all=r(p)
boottest TREAT, nograph
local pvalue_wcb = r(p)

eststo table2_LPM, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')

// // Exporting the regression results
xml_tab table2_LPM , save($output/Table_2.xls) sheet(LPM_Nem) sd2 replace stats(ritest_p means_all pvalue_all pvalue_wcb) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(Table 2 LPM Nematode Results) 



// ORDERED PROBIT	
oprobit change_tox TREAT if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
eststo table2_NemOP
	
foreach delta in 3 2 1 {
estimates restore table2_NemOP
margins, dydx(TREAT) predict(outcome(-`delta')) post
eststo _table2_op_neg`delta'
}
foreach delta in 0 1 2 3 {
estimates restore table2_NemOP
margins, dydx(TREAT) predict(outcome(`delta')) post
eststo _table2_op_`delta'
}

xml_tab _table2_op_* , save($output/Table_2.xls) append sheet(OP_Nem) sd2 ///
	format(SCLR3 NCLR3) title(Table 2 OP Nematode Results) 
	
eststo clear
	
	// save file for robustness check later
save "${temp}/_tableTwoNem.dta", replace
		

		

	 ////////////////////////


//////  Bollworm Choices  ////////
use "${temp}\Rd1_BwChoices_tox.dta", clear
joinby FMR Scenario_Number using "${temp}\Rd2_BwChoices_tox.dta", unmatched(both) _merge(_1)

tab _1 // unmatched observations had wrong block in either round

/// add blocking variables if for difference regression
rename block block1
joinby EA using "${Round1}\treatment.dta", unmatched(both) _merge(_2)
drop _2

gen replacement = 0 
replace replacement = 1 if FMR==1123|FMR==1168|FMR==2076|FMR==2114|FMR==2142|FMR==3002|FMR==3003
gen wrong_block2 = 0
replace wrong_block2 = 1 if FMR ==1397|FMR==1414|FMR==2057|FMR==2099|FMR==2116|FMR==3006|FMR==3042

// create variable for price differences across rounds - BW scenarios
gen wrong_price = 0
replace wrong_price = 1 if design==2 & Scenario_Number==4  // note this is block 2 design 2
replace wrong_price = 1 if (design==3 & Scenario_Number==1) |(design==3 & Scenario_Number==2) // note these are block 1 & block 2 in design 3

clonevar TREATMENT = treat
/// expand to facilitate DiD regressions
expand 2
by FMR Scenario_Number, sort : egen float _2 = seq(), from(1) to(10) block(1)

gen ToxScore = Rd1_Tox if _2==1
replace ToxScore = Rd2_Tox if _2==2
replace ToxScore=. if Rd1_Tox==.|Rd2_Tox==.

rename _2 rd
replace rd = 0 if rd==1
replace rd = 1 if rd==2

//// CHANGE IN TOX VARIABLE  - First-difference estimation
gen change_tox = (Rd2_Tox - Rd1_Tox)

			
			/////////   TABLE 2 BOLLWORMS ////////////
			
// Regression 1 - LPM

// random inference tests
ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(321) :  reg change_tox TREAT i.block if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
matrix LPM_rit = r(p)
local ritest_p = LPM_rit[1,1]

// means
sum Rd1_Tox if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1
local means_all = r(mean)

// econometric output
reg change_tox TREAT i.block if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
testparm TREAT
local pvalue_all=r(p)
boottest TREAT, nograph
local pvalue_wcb = r(p)

eststo table2_LPM, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')

// // Exporting the regression results
xml_tab table2_LPM , save($output/Table_2.xls) sheet(LPM_Bollworm) sd2 append stats(ritest_p means_all pvalue_all pvalue_wcb) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(Table 2 LPM Bollworm Results) 



// ORDERED PROBIT	
oprobit change_tox TREAT if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
eststo table2_BwOP
	
foreach delta in 3 2 1 {
estimates restore table2_BwOP
margins, dydx(TREAT) predict(outcome(-`delta')) post
eststo _table2_op_neg`delta'
}
foreach delta in 0 1 2 3 {
estimates restore table2_BwOP
margins, dydx(TREAT) predict(outcome(`delta')) post
eststo _table2_op_`delta'
}

xml_tab _table2_op_* , save($output/Table_2.xls) append sheet(OP_Bollworm) sd2 ///
	format(SCLR3 NCLR3) title(Table 2 OP Bollworm Results) 
	
eststo clear	
	
// save file for robustness check later
save "${temp}/_tableTwoBw.dta", replace
			


////////////////////////////////////////////////////////

			// 	TABLE 3		///

////////////////////////////////////////////////////////

// CONDITIONAL LOGIT ESTIMATIONS  ///
// 1. Nematodes
// 2. Bollworms

*****************
**             **
**  NEMATODES  **        
**             **
*****************
//////////////////

//

//  open Baseline nematode file 
use "${temp}\Rd1_NematodeChoiceData(Jan7).dta", clear

rename block Block
rename scen Scenario_Number
rename fmr FMR
rename nematicide_* Nematicide*
rename no_ No_Nematicide
drop if Sc==.

gen round = 0

// append Rd2 dataset   /// data sets match on FMR Scenario_Number after Replacement, wrong_block, wrong_price are removed - confirmed with joinby command.
append using "${temp}\Rd2_NemChoiceData(01.08.16).dta"

replace round=1 if round==.

///////// need to drop observations with no choice 
list FMR Sce if Nematicide1==.& Nematicide2==.& Nematicide3==.& No_Nematicide==.
gen no_choice = 0
replace no_choice=1 if (FMR==1201 & Scen==19)|(FMR==1060 & Scen==16)|(FMR==1064 & Scen==20)|(FMR==1374 & Scen==18)|(FMR==1380 & Scen==18)|(FMR==1087 & Scen==20)|(FMR==2192 & Scen==16)/*
*/|(FMR==2142 & Scen==9)|(FMR==1138 & Scen==18)|(FMR==2131 & Scen==16)|(FMR==3003 & Scen==16)|(FMR==1390 & Scen==16)|(FMR==1125 & Scen==19)|(FMR==1308 & Scen==19)

// create variables for unmatched/incorrect observations
gen replacement = 0 
replace replacement = 1 if FMR==1123|FMR==1168|FMR==2076|FMR==2114|FMR==2142|FMR==3002|FMR==3003
gen wrong_block2 = 0
replace wrong_block2 = 1 if FMR ==1397|FMR==1414|FMR==2057|FMR==2099|FMR==2116|FMR==3006|FMR==3042

// create variable for price differences across rounds
gen wrong_price = 0
replace wrong_price =1 if design==1 & Scenario_Number==6
replace wrong_price =1 if design==1 & Scenario_Number==13
replace wrong_price =1 if design==2 & Scenario_Number==6

gen out=0 
replace out=1 if replacement==1|wrong_block==1|wrong_price==1|no_choice==1
// tab round if out!=1 // balanced


gen id= _n    //  FL: creates a caseID for rows

//  add the design levels from pressure and prices
merge m:1 design Scenario_Number using "${temp}\nematodes_designALL.dta"

rename i4price4 i3price3

// go from 1 row per id to 4 rows per id
expand 4
sort id

//  code dependent variable
*drop choice_q2
gen choice_q2=0
replace choice_q2=1 if Nematicide1 ==1
replace choice_q2=2 if Nematicide2 ==1 
replace choice_q2=3 if Nematicide3 ==1
replace choice_q2=4 if No_Nematicide==1

//  create dummy indicator of which alternative was chosen 
by id: gen choice2 = choice_q2 == _n 

// lable the rows for the 1,2,3,...,6 alternatives in each choice set
// 1=I1, 2=I2, ... 7=None 
by id: gen alternative = _n



// create and recode the choice2 rhs variables
*drop price
gen price =0
replace price=i1price1 if alternative==1
replace price=i2price2 if alternative==2
replace price=i3price3 if alternative==3
tab price alternative

rename ea EA

// // join in farmer characteristics
// joinby FMR using "${temp}\FMRcharacteristics.dta", unmatched(both) _merge(_6)


gen TREATMENT = 0
replace TREATMENT = 1 if EA==4|EA==5|EA==6|EA==8|EA==14|EA==15|EA==16|EA==17|EA==22|EA==23|EA==25|EA==27|EA==32|EA==33|EA==34|EA==37

gen Tp = TREAT*price

/// RESULTS 
/// BASELINE ESTIMATION
asclogit choice2 price Tp if round==0 & out==0 , case(id) alt(alternative) casevars(TREATMENT) basealternative(2) vce(cluster EA)
quietly estat mfx, varlist(TREAT)
eststo margins
foreach x in 1 2 3  {
	matrix mat_`x' = r(_`x')
	local dpdx`x' = mat_`x'[1,1]
	local se`x' = mat_`x'[1,2]
	local pvalue`x' = mat_`x'[1,4]
}

quietly asclogit choice2 price Tp if round==0 & out==0 , case(id) alt(alternative) casevars(TREATMENT) basealternative(2) vce(cluster EA)
eststo table3_Nem_base, addscalars(Umet_dpdx `dpdx1' Umet_se `se1' Umet_p `pvalue1' Orizon_dpdx `dpdx2' Orizon_se `se2' Orizon_p `pvalue2' BioNematon_dpdx `dpdx3' BioNematon_se `se3' BioNematon_p `pvalue3')


/// ENDLINE ESTIMATION
quietly asclogit choice2 price Tp if round==1 & out==0 , case(id) alt(alternative) casevars(TREATMENT) basealternative(2) vce(cluster EA)
quietly estat mfx, varlist(TREAT)
eststo margins_end
foreach x in 1 2 3 4 5 6 {
	matrix mat_`x' = r(_`x')
	local dpdx`x' = mat_`x'[1,1]
	local se`x' = mat_`x'[1,2]
	local pvalue`x' = mat_`x'[1,4]
}

quietly asclogit choice2 price Tp if round==1 & out==0 , case(id) alt(alternative) casevars(TREATMENT) basealternative(2) vce(cluster EA) 
eststo table3_Nem_end, addscalars(Umet_dpdx `dpdx1' Umet_se `se1' Umet_p `pvalue1' Orizon_dpdx `dpdx2' Orizon_se `se2' Orizon_p `pvalue2' BioNematon_dpdx `dpdx3' BioNematon_se `se3' BioNematon_p `pvalue3')


xml_tab table3_Nem* , save($output/Table_3.xls) sheet(Nematode) sd2 replace stats(N Umet_dpdx Umet_se Umet_p Orizon_dpdx Orizon_se Orizon_p BioNematon_dpdx BioNematon_se BioNematon_p) ///
	format(SCLR3 NCLR3) keep(price Tp) title(Table 3 Nematode Results) 

	
// save file for robustness check later
save "${temp}/_tableThreeNem.dta", replace

*****************
**             **
**  Bollworms  **        
**             **
*****************
//////////////////


//  open bollworm file 
use "${temp}\Rd1_BollwormChoiceData(Jan7).dta", clear

// rename variables to match Rd2 dataset
rename fmr FMR
rename Insecticide_* Insecticide*

gen round =0
drop if Scen==.

// append datasets   /// data sets match on FMR Scenario_Number after Replacement, wrong_block, wrong_price are removed - confirmed with joinby command.
append using "${temp}\Rd2_BwChoiceData(06.08.16).dta"

replace round=1 if round==.

///////// need to drop observations with no choice selected. 20 in baseline, 2 in endline.
list FMR Sce if Insecticide1==.& Insecticide2==.& Insecticide3==.& Insecticide4==.& Insecticide5==.& Insecticide6==.& No_Insecticide==.
gen no_choice = 0
replace no_choice=1 if (FMR==2038 & Scen==9)|(FMR==1407 & Scen==15)|(FMR==1297 & Scen==19)|(FMR==1085 & Scen==13)|(FMR==1374 & Scen==18)|(FMR==1056 & Scen==18)/*
*/|(FMR==1064 & Scen==17)|(FMR==1390 & Scen==20)|(FMR==1132 & Scen==20)|(FMR==3131 & Scen==18)|(FMR==3016 & Scen==16)|(FMR==3115 & Scen==20)|(FMR==2194 & Scen==1)/*
*/|(FMR==2194 & Scen==4)|(FMR==2194 & Scen==12)|(FMR==2194 & Scen==16)|(FMR==2092 & Scen==10)|(FMR==2192 & Scen==13)|(FMR==2008 & Scen==9)|(FMR==2067 & Scen==15)/*
*/|(FMR==1125 & Scen==19)|(FMR==1308 & Scen==19)

/// Create variables to exclude observations
gen replacement = 0 
replace replacement = 1 if FMR==1123|FMR==1168|FMR==2076|FMR==2114|FMR==2142|FMR==3002|FMR==3003
gen wrong_block2 = 0
replace wrong_block2 = 1 if FMR ==1397|FMR==1414|FMR==2057|FMR==2099|FMR==2116|FMR==3006|FMR==3042

// create variable for price differences across rounds - BW scenarios
gen wrong_price = 0
replace wrong_price = 1 if design==2 & Scenario_Number==4  // note this is block 2 design 2
replace wrong_price = 1 if (design==3 & Scenario_Number==1) |(design==3 & Scenario_Number==2) // note these are block 1 & block 2 in design 3

gen out=0 
replace out=1 if replacement==1|wrong_block==1|wrong_price==1|no_choice==1

/// CHECK BALANCE ACROSS ROUNDS
tab round if out!=1   // Balanced.

gen id= _n    //  FL: creates a caseID for rows

//  add the design levels from pressure and prices
merge m:1 design Scenario_Number using "${temp}\Rd1_bollworms_designALL_JG.dta"

// go from 1 row per id to 7 rows per id
expand 7
sort id

//  code dependent variable
*drop choice_q1
gen choice_q1=0
replace choice_q1=3 if Insecticide3 ==1
replace choice_q1=2 if Insecticide2 ==1 
replace choice_q1=4 if Insecticide4 ==1
replace choice_q1=5 if Insecticide5 ==1
replace choice_q1=1 if Insecticide1 ==1
replace choice_q1=6 if Insecticide6 ==1
replace choice_q1=7 if No_Insecticide==1

drop if choice_q1==0   // 19 scenarios with no choice, and 128 scenarios with no choice from Miloso removed (4 scenarios instead of 5)


//  create dummy indicator of which alternative was chosen 
by id: gen choice1 = choice_q1 == _n 

// lable the rows for the 1,2,3,...,6 alternatives in each choice set
// 1=I1, 2=I2, ... 7=None 
by id: gen alternative = _n

gen price =0
replace price=i1price1 if alternative==1
replace price=i2price2 if alternative==2
replace price=i3price3 if alternative==3
replace price=i4price4+i5price5 if alternative==4
replace price=i5price5 if alternative==5
replace price=i6price6+i1price1 if alternative==6

gen none=0
replace none=1 if alternative==7  // dummy for the treat/don't treat option

rename ea EA
rename block _block
// join in farmer characteristics
joinby FMR using "${temp}\FMRcharacteristics.dta", unmatched(both) _merge(_6)

// create necessary interaction terms
gen Tp = TREAT*price


/// RESULTS 


// BASELINE ESTIMATION
quietly asclogit choice1 price Tp if round==0 & out!=1, case(id) alt(alternative) casevars(TREAT) basealternative(3) vce(cluster EA) 
estat mfx, varlist(TREAT)
eststo margins
foreach x in 1 2 3 4 5 6 {
	matrix mat_`x' = r(_`x')
	local dpdx`x' = mat_`x'[1,1]
	local se`x' = mat_`x'[1,2]
	local pvalue`x' = mat_`x'[1,4]
}

quietly asclogit choice1 price Tp if round==0 & out!=1, case(id) alt(alternative) casevars(TREAT) basealternative(3) vce(cluster EA) 
eststo table3_Bw_base, addscalars(Phoskill_dpdx `dpdx1' Phoskill_se `se1' Phoskill_p `pvalue1' Benefit_dpdx `dpdx2' Benefit_se `se2' Benefit_p `pvalue2' ///
	Profenofos_dpdx `dpdx3' Profenofos_se `se3' Profenofos_p `pvalue3' Bollpack_dpdx `dpdx4' Bollpack_se `se4' Bollpack_p `pvalue4' ///
	Blast_dpdx `dpdx5' Blast_se `se5' Blast_p `pvalue5' AlphaGold_dpdx `dpdx6' AlphaGold_se `se6' AlphaGold_p `pvalue6')

 
	
// ENDLINE ESTIMATION
quietly asclogit choice1 price Tp if round==1 & out!=1, case(id) alt(alternative) casevars(TREAT) basealternative(3) vce(cluster EA) 
quietly estat mfx, varlist(TREAT)
eststo margins_end
foreach x in 1 2 3 4 5 6 {
	matrix mat_`x' = r(_`x')
	local dpdx`x' = mat_`x'[1,1]
	local se`x' = mat_`x'[1,2]
	local pvalue`x' = mat_`x'[1,4]
}

quietly asclogit choice1 price Tp if round==1 & out!=1, case(id) alt(alternative) casevars(TREAT) basealternative(3) vce(cluster EA) 
eststo table3_Bw_end, addscalars(Phoskill_dpdx `dpdx1' Phoskill_se `se1' Phoskill_p `pvalue1' Benefit_dpdx `dpdx2' Benefit_se `se2' Benefit_p `pvalue2' ///
	Profenofos_dpdx `dpdx3' Profenofos_se `se3' Profenofos_p `pvalue3' Bollpack_dpdx `dpdx4' Bollpack_se `se4' Bollpack_p `pvalue4' ///
	Blast_dpdx `dpdx5' Blast_se `se5' Blast_p `pvalue5' AlphaGold_dpdx `dpdx6' AlphaGold_se `se6' AlphaGold_p `pvalue6')


xml_tab table3_Bw* , save($output/Table_3.xls) sheet(Bollworm) sd2 append stats(Phoskill_dpdx Phoskill_se Phoskill_p Benefit_dpdx Benefit_se Benefit_p  ///
	Profenofos_dpdx Profenofos_se Profenofos_p Bollpack_dpdx Bollpack_se Bollpack_p  ///
	Blast_dpdx Blast_se Blast_p AlphaGold_dpdx AlphaGold_se AlphaGold_p ) ///
	format(SCLR3 NCLR3) keep(price Tp) title(Table 3 Bollworm Results) 


// save file for robustness check later
save "${temp}/_tableThreeBw.dta", replace






////////////////////////////////////////////////////////

			// 	TABLE 4		///

////////////////////////////////////////////////////////


// // ROUND 2 PESTICIDE PURCHASES // 
	
use "Round2\PesticidePurchases_Codes.dta", clear

joinby CODE using "${temp}\WHO_codes.dta", unmatched(both) _merge(_1)
drop if FMR==.

	//create BW and Nematicide indicators
gen bw = 0
foreach i in 7 10 14 15 16 17 22 31 32 33 37 38 41 43 {
	replace bw = 1 if CODE==`i'
	}
	
gen nem = 0
foreach i in 20 39 44 45 {
	replace nem = 1 if CODE==`i'
	}

gen Ib = who==1
gen II = who==2
gen U = who==4
gen _pest=1 // variable for if HH purchased any pesticide at baseline
gen _insect = type==2|type==4
gen _abamectin = CODE==1

foreach var of varlist bw nem _insect {
	gen `var'_Ib = `var' if who==1
	gen `var'_II = `var' if who==2
	gen `var'_U = `var' if who==4
	}

collapse (sum) _insect _pest _abamectin bw nem bw_Ib bw_II bw_U nem_Ib nem_II nem_U _insect_Ib _insect_II _insect_U, by(FMR)

joinby FMR using "${temp}\FMRcharacteristics.dta", unmatched(both) _merge(_merge)
drop _merge
recode _pest _insec* bw* nem* _abamectin (. = 0)

// save file for robustness check later
save "${temp}/_tableFour.dta", replace

foreach outcome in _insect _insect_Ib _insect_II _insect_U bw bw_Ib bw_II bw_U nem nem_Ib nem_II nem_U {

	// random inference tests
	ritest TREAT _b[TREAT] , cluster(EA) reps(1000) seed(111) :  areg `outcome' TREAT if replace!=1,  cluster(EA) a(block)	
	matrix `outcome'_rit = r(p)
	local ritest_p = `outcome'_rit[1,1]

	// means
	areg `outcome' TREAT if replace!=1 & TREAT == 0,  cluster(EA) a(block)	 
	matrix `outcome'_mean = e(b)
	local means_all = `outcome'_mean[1,2]
	
	// econometric output
	areg `outcome' TREAT if replace!=1,  cluster(EA) a(block)
	testparm TREAT
	local pvalue_all=r(p)
	boottest TREAT, nograph
	local pvalue_wcb = r(p)
	
	eststo table4_`outcome'_reg1, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')	
}

// Exporting the regression results
xml_tab table4_* , save($output/Table_4.xls) sd2 replace stats(N r2 pvalue_all pvalue_wcb ritest_p means_all) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(Table 4 Regression Results) 



////////////////////////////////////////////////////////

			// 	TABLE 5		///

////////////////////////////////////////////////////////


/////////////////////////////////////////////////////
*****************
**                								  **
** //	ACUTAL PESTICIDE PURCHASES                **        
**//		- MOST ANALYSIS DONE IN EXCEL -- FINISH MOVING TO HERE             **        
*****************
/////////////////////////////////////////////////////


////////////////////////////

////////////////////////////

// CHECK TOXICITIES OF PURCHASED PESTICIDES at RD2


/////////////////////////////////////

// // ROUND 1 PESTICIDE PURCHASES // 
clear all

use "${Round1}\3.1codes.dta", clear

joinby FMR using "${temp}\FMRcharacteristics.dta", unmatched(both) _merge(_merge)
drop if FMR==.
drop _merge

joinby FMR using "${temp}\trainers.dta", unmatched(both) _merge(_2)
drop _2

joinby CODE using "${temp}\WHO_codes.dta", unmatched(both) _merge(_1)
drop if FMR==.
drop _1

	//create BW and Nematicide indicators
gen bw = inlist(CODE,7,10, 14, 15,16, 17, 22, 31, 32, 33, 37, 38, 41, 43)
gen nem = inlist(CODE, 20, 39, 44, 45)

/// RESTRICT RD1 to plots coinciding with timing of Rd2 survey
forvalues i = 1(1)4 {
	gen plot`i'= PLOT1==`i' | PLOT2==`i' | PLOT3==`i' | PLOT4==`i'
	}
	
capture program drop plantTime
program define plantTime
	preserve

	use "${Round1}\2.1.dta", clear
	foreach i in 5 6 7 8 9 {
		gen t_`i' = TP06==`i'
		}
	gen t_match = TP06>=5 & TP06<=9	
	
	quietly tabulate PLOT, generate(_plot)
	keep if t_match==1
	
	keep EA FMR _plot* t_*
	tempfile rd1_plant
	save `rd1_plant'
	
	restore
	
	merge m:m FMR using `rd1_plant' 
	drop _merge
	
end

plantTime

/// RESTRICT RD1 to farmers that also made purchases in Rd2
	
capture program drop rd2Match
program define rd2Match
	preserve

	use "${Round2}\PesticidePurchases_Codes.dta", clear
	collapse (count) CODE, by(FMR)
	rename CODE rd2N
	
	keep FMR rd2N
	tempfile rd2_match
	save `rd2_match'
	
	restore
	
	merge m:1 FMR using `rd2_match', keep(3) 
	drop _merge
	
end

rd2Match

gen __in =0
foreach v in plot1 plot2 plot3 plot4 {	
	replace __in =1 if `v'==1 & _`v'==1
	}

drop if __in !=1|_trainer==1|replacement==1

// tables with chi-square tests
tab who TREAT if bw==1, chi2 exact
gen chiSqBw = r(chi2)
gen chiSqBw_p = r(p)

tab who TREAT if nem==1

// save file for robustness check later
save "${temp}/_tableFiveRd1.dta", replace

foreach pest in bw nem {
	gen `pest'_high = `pest'==1 & who==1
	gen `pest'_mid = `pest'==1 & (who==2|who==3)
	gen `pest'_low = `pest'==1 & who==4
	}

collapse (sum)bw_* nem_* (first) chiSq*, by(TREAT)

foreach pest in bw nem {
	egen `pest'_tot = rowtotal(`pest'_*)
	foreach tox in high mid low {
		gen `pest'_`tox'_share = `pest'_`tox'/`pest'_tot
		}
	}
keep TREAT bw_tot bw_high_share-nem_low_share chi*
order chi*, last
xpose, clear

mkmat v1 v2, matrix(baseline)

putexcel set "$output/Table_5.xlsx", sheet("baseline") replace
putexcel A1=("Baseline purchases") A2=("Treatment status") A3=("N bw") A4 = ("BW-High Share") A5=("BW-Mid Share") A6=("BW-Low Share") ///
	A7=("N nem") A8=("Nem-High Share") A9=("Nem-Mid Share") A10=("Nem-Low share") A11=("BW Chi-square") A12=("BW Chi-square p-value")
putexcel B2=matrix(baseline), nformat(number_d2)

/////////////////////////////////////

// // ROUND 2 PESTICIDE PURCHASES // 
	
use "Round2\PesticidePurchases_Codes.dta", clear
joinby FMR using "${temp}\trainers.dta", unmatched(both) _merge(_2)
joinby FMR using "${temp}\FMRcharacteristics.dta", unmatched(both) _merge(_merge)
drop if FMR==.
drop _merge

joinby CODE using "${temp}\WHO_codes.dta", unmatched(both) _merge(_1)
drop if FMR==.

	//create BW and Nematicide indicators
gen bw = inlist(CODE,7,10, 14, 15,16, 17, 22, 31, 32, 33, 37, 38, 41, 43)
gen nem = inlist(CODE, 20, 39, 44, 45)
drop if _trainer==1|replacement==1

// tables with chi-square tests
tab who TREAT if bw==1, chi2 exact
gen chiSqBw = r(chi2)
gen chiSqBw_p = r(p)

tab who TREAT if nem==1, chi2 exac
gen chiSqNem = r(chi2)
gen chiSqNem_p = r(p)

// save file for robustness check later
save "${temp}/_tableFiveRd2.dta", replace

preserve
// save FMRs for balance tests
collapse (sum) bw nem, by(FMR)
keep FMR bw nem

// save file for robustness check later
save "${temp}/_tableFive.dta", replace
restore

foreach pest in bw nem {
	gen `pest'_high = `pest'==1 & who==1
	gen `pest'_mid = `pest'==1 & (who==2|who==3)
	gen `pest'_low = `pest'==1 & who==4
	}

collapse (sum)bw_* nem_* (first) chiSq*, by(TREAT)

foreach pest in bw nem {
	egen `pest'_tot = rowtotal(`pest'_*)
	foreach tox in high mid low {
		gen `pest'_`tox'_share = `pest'_`tox'/`pest'_tot
		}
	}
keep TREAT bw_tot bw_high_share-nem_low_share chi*
order chi*, last
xpose, clear

mkmat v1 v2, matrix(endline)

putexcel set "$output/Table_5.xlsx", sheet("endline") modify
putexcel A1=("Endline purchases") A2=("Treatment status") A3=("N bw") A4 = ("BW-High Share") A5=("BW-Mid Share") A6=("BW-Low Share") ///
	A7=("N nem") A8=("Nem-High Share") A9=("Nem-Mid Share") A10=("Nem-Low share") A11=("BW Chi-square") A12=("BW Chi-square p-value") ///
	A13=("Nem Chi-square") A14=("Nem Chi-square p-value")
putexcel B2=matrix(endline), nformat(number_d2)


	
////////	***********		///////////		**********		////////////

////////	***********		///////////		**********		////////////


////////	***********		///////////		**********		////////////

////////	***********		///////////		**********		////////////





//////////			SECTION 2: PPE DEMAND



			
////////	***********		///////////		**********		////////////

////////	***********		///////////		**********		////////////


////////	***********		///////////		**********		////////////

////////	***********		///////////		**********		////////////




// 						CREATE DATA FILE & VARIABLES

////////////   ------------------  ///////////////////  -----------------//////////////////
use "${Round2}\BDM.dta", clear
joinby FMR using "${temp}\FMRcharacteristics.dta", unmatched(both) _merge(_1)
joinby FMR using "${temp}\Income_PCA.dta", unmatched(both) _merge(_2)
joinby FMR using "${temp}\trainers.dta", unmatched(both) _merge(_3)
rename _trainer trainer
joinby FMR using "${Round2}\PPEuse.dta", unmatched(both) _merge(_4)
joinby FMR using "${temp}\Rd1_PPE.dta", unmatched(both) _merge(_5)
joinby FMR using "${temp}\Rd1_KnowledgeScore_11.1.16.dta", unmatched(both) _merge(_6)
joinby FMR using "${temp}\Rd2_KnowledgeScore_11.1.16.dta", unmatched(both) _merge(_7)
joinby FMR using "${Round2}\ScenarioPerc_Decision.dta", unmatched(both) _merge(_8)
joinby FMR using "${temp}\Rd1_TomArea.dta", unmatched(both) _merge(_9)
joinby FMR using "${temp}\Rd1_PesticideToxicity.dta", unmatched(both) _merge(_10)
joinby FMR using "${temp}\Rd1_Mix_Apply.dta", unmatched(both) _merge(_11)
joinby FMR using "${Round1}\1.2.dta", unmatched(both) _merge(_12)
joinby EA using "${temp}\Rd1_EAprices.dta", unmatched(both) _merge(_13)
joinby FMR using "${temp}\Rd1_FMRpests.dta", unmatched(both) _merge(_14)
joinby EA using "${temp}\Rd1_TOMprices.dta", unmatched(both) _merge(_15)
joinby FMR using "${temp}\trim_BDM.dta", unmatched(both) _merge(_16)
joinby FMR using "${temp}\glove_mask_perceptions.dta", unmatched(both) _merge(_17)
joinby FMR using "${temp}\HHmemTomatoPlots.dta", unmatched(both) _merge(_18)
drop _*
 
 // Baseline and Endline PPE Ownership variables
gen bgloves = (own_gloves) if !missing(own_gloves)
gen egloves = (PPE04==1) if !missing(PPE04)
gen bmask = (own_mask) if !missing(own_mask)
gen emask = (PPE07==1) if !missing(PPE07)
gen bboots = (own_boots) if !missing(own_boots)
gen eboots = (PPE01==1) if !missing(PPE01)
gen bsuit = (own_full) if !missing(own_full)
gen esuit = (PPE13==1) if !missing(PPE13)
gen bgoggles = (own_goggles) if !missing(own_goggles)
gen egoggles = (PPE10==1) if !missing(PPE10)

// create dummies for PPE ownership at endline survey
gen gloves = (bgloves==1|egloves==1)
gen mask_own = (bmask==1|emask==1)
gen boots = (bboots==1|eboots==1)
gen suit = (bsuit==1|esuit==1)
gen goggles = (bgoggles==1|egoggles==1)


// create dummy variable for receiving either type of treatment
gen _treated = (training==1|letter==1)
// create complier dummy variable
gen complier = (training==1&letter==1)


/// CREATE ADD'L VARIABLES FOR ESTIMATIONS   
// create PPE knowledge variable
gen k_ppe = (K2_gloves + K2_mask)
gen k_ppe1 = (K1_gloves + K1_mask)

// include relevant covariates - education, info access, (imbalances), inc_pca, prior ownership of PPE, HEALTH?, whether or not the farmer applies/mixes themselves
joinby FMR using "${temp}\Rd1_SourcesInfo.dta", unmatched(both) _merge(_9)
joinby FMR using "${temp}\Rd1_AcuteSymptoms.dta", unmatched(both) _merge(_10)
joinby FMR using "${temp}\Rd2_AcuteSymptoms.dta", unmatched(both) _merge(_11)
joinby FMR using "${Round1}\1.2.dta", unmatched(both) _merge(_12)

//// create a single variable for in sample
gen insample = (replace!=1)
gen trimsample = (TRIM!=1 & replace!=1 & trim_BDMm !=1 & trim_BDMg!=1 & block!=2 & block!=15)

/// outcome variable means for the control group
sum G_bid_final if TREAT==0 & insample==1, det
sum M_bid_final if TREAT==0 & insample==1, det

// PPE and Tox health knowledge variables
gen k_U = DEC_07==4|DEC_07==5  // say class U has low health risk
gen k_Ib = DEC_06==1|DEC_06==2  // say class Ib has high health risk
tab k_U k_Ib 

/// create a variable for perception that both pesticides are highly toxic
gen toxHigh = (DEC_07==1|DEC_07==2)&(DEC_06==1|DEC_06==2)

/// Variable for relative toxicity knowledge - know that Ib is highly toxic and U is not toxic
gen kIbU = k_U * k_Ib

/// camp and block dummies
quietly tabulate camp, gen(z)
quietly tabulate block, gen(b)

/////  EXPAND  DATA TO FACILITATE POOLED ESTIMATION  ////
expand 2, gen(id)

gen bid = M_bid_final if id ==1
replace bid = G_bid_final if id==0

// create positive bid variable for Cragg DH model
gen b_pos = bid>0
gen mask = id
recode BDM 2=0

gen first = 0
replace first = 1 if (mask==0 & BDM==1)|(mask==1 & BDM==0)

// create endline pesticide application dummy
gen e_pesticide = nopesticide_Rd2==0 


////////////////////////////////////////////////////////////////////////////


// /////////////////          Figure 1. PPE DEMAND CURVES			////////////////


/////////////////////////////////////////////////////////////////////////////
// export data to excel, easier for tables
tab M_bid_final if mask==1 & insample==1
tab G_bid_final if mask==0 & insample==1

////////////   ------------------  ///////////////////  -----------------//////////////////

////////////   ------------------  ///////////////////  -----------------//////////////////


////// //////////////// ////// //////////////// ////// //////////////// ////// //////////////// 
 
////             Table 6. ITT ESTIMATES	on BDM bids		 				     ////// ////////////////

////// //////////////// ////// //////////////// ////// //////////////// ////// ////////////////

/// 		Mask and Gloves Separately

forvalues x = 0(1)1 {
	// random inference tests
	ritest TREAT _b[TREAT] , cluster(EA) reps(1000) seed(111) :  areg bid TREAT if mask==`x' & insample==1,  cluster(EA) a(block)	
	matrix bid_rit = r(p)
	local ritest_p = bid_rit[1,1]

	// means
	areg bid TREAT if mask==`x' & insample==1 & TREAT==0,  cluster(EA) a(block)	 
	matrix bid_mean = e(b)
	local means_all = bid_mean[1,2]
	
	// econometric output
	areg bid TREAT if mask==`x' & insample==1,  cluster(EA) a(block)
	testparm TREAT
	local pvalue_all=r(p)
	boottest TREAT, nograph
	local pvalue_wcb = r(p)
	
	eststo table6_MaskEqual_`x'_reg1, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')	
	
	// with covariate controls
		// random inference tests
	ritest TREAT _b[TREAT] , cluster(EA) reps(1000) seed(111) :  areg bid TREAT Bus read HORT_all if mask==`x' & insample==1,  cluster(EA) a(block)	
	matrix bid_rit = r(p)
	local ritest_p = bid_rit[1,1]

	// means
	areg bid TREAT if mask==`x' & insample==1 & TREAT==0,  cluster(EA) a(block)	 
	matrix bid_mean = e(b)
	local means_all = bid_mean[1,2]
	
	// econometric output
	areg bid TREAT Bus read HORT_all if mask==`x' & insample==1,  cluster(EA) a(block)
	testparm TREAT
	local pvalue_all=r(p)
	boottest TREAT, nograph
	local pvalue_wcb = r(p)
	
	eststo table6_MaskEqual_`x'_reg2, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')	
	
}

// Exporting the regression results
xml_tab table6_* , save($output/Table_6.xls) sd2 replace stats(N r2 pvalue_all pvalue_wcb ritest_p means_all) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(Table 6 Regression Results) 


// save file for use later
save "${temp}/_tableSix.dta", replace

	
////// //////////////// ////// //////////////// ////// //////////////// ////// //////////////// 
 
////              Table 7. Heterogeneous effects of information 		     ////// ////////////////

////// //////////////// ////// //////////////// ////// //////////////// ////// ////////////////

use "${temp}/_tableSix.dta", clear

forvalues x = 0(1)1 {
	foreach cov in Ib U {
		// random inference tests
		
		ritest TREAT _b[1.TREAT#c.`cov'] , cluster(EA) reps(1000) seed(111) :  areg bid i.TREAT##c.`cov' if mask==`x' & insample==1,  cluster(EA) a(block)	
		matrix _rit = r(p)
		local ritest_p = _rit[1,1]

		// means
		sum `cov' if mask==`x' & insample==1 & TREAT==0	 
		local means_1 = r(mean)
	
		// econometric output
		areg bid i.TREAT##c.`cov' if mask==`x' & insample==1,  cluster(EA) a(block)
		testparm 1.TREAT
		local pvalue_treat=r(p)
		testparm c.`cov'
		local pvalue_cov=r(p)
		testparm 1.TREAT#c.`cov'
		local pvalue_Int=r(p)
		
		
		boottest 1.TREAT, nograph
		local pvalue_wcb_treat = r(p)
		boottest c.`cov', nograph
		local pvalue_wcb_cov = r(p)
		boottest 1.TREAT#c.`cov', nograph
		local pvalue_wcb_Int = r(p)
		
		
	eststo table7_`cov'_mask`x'_1, addscalars(ritest_p `ritest_p' means_all `means_1' pvalue_treat `pvalue_treat' ///
		pvalue_cov `pvalue_cov' pvalue_Int `pvalue_Int' pvalue_wcb_treat `pvalue_wcb_treat' pvalue_wcb_cov `pvalue_wcb_cov' ///
		pvalue_wcbInt `pvalue_wcb_Int')	
	
			// with interaction terms
		ritest TREAT _b[1.TREAT#c.`cov'] , cluster(EA) reps(1000) seed(111) :  areg bid i.TREAT##c.`cov' Bus HORT_all read if mask==`x' & insample==1,  cluster(EA) a(block)	
		matrix _rit = r(p)
		local ritest_p = _rit[1,1]

		// means
		sum `cov' if mask==`x' & insample==1 & TREAT==0	 
		local means_2 = r(mean)
	
		// econometric output
		areg bid i.TREAT##c.`cov' Bus HORT_all read if mask==`x' & insample==1,  cluster(EA) a(block)
		testparm 1.TREAT
		local pvalue_treat=r(p)
		testparm c.`cov'
		local pvalue_cov=r(p)
		testparm 1.TREAT#c.`cov'
		local pvalue_Int=r(p)
		
		
		boottest 1.TREAT, nograph
		local pvalue_wcb_treat = r(p)
		boottest c.`cov', nograph
		local pvalue_wcb_cov = r(p)
		boottest 1.TREAT#c.`cov', nograph
		local pvalue_wcb_Int = r(p)
		
		
	eststo table7_`cov'_mask`x'_2, addscalars(ritest_p `ritest_p' means_all `means_2' pvalue_treat `pvalue_treat' ///
		pvalue_cov `pvalue_cov' pvalue_Int `pvalue_Int' pvalue_wcb_treat `pvalue_wcb_treat' pvalue_wcb_cov `pvalue_wcb_cov' ///
		pvalue_wcbInt `pvalue_wcb_Int')	
		
		
		}
	}


// Exporting the regression results
xml_tab table7_* , save($output/Table_7.xls) sd2 replace stats(N r2 pvalue_treat pvalue_cov pvalue_Int pvalue_wcb_treat ///
	pvalue_wcb_cov	pvalue_wcbInt ritest_p means_all)format(SCLR3 NCLR3) ///
	keep(1.TREATMENT Ib 1.TREATMENT#c.Ib U 1.TREATMENT#c.U) title(Table 7 Regression Results) 


			
////////	***********		///////////		**********		////////////

////////	***********		///////////		**********		////////////


//////////			APPENDIX TABLES

			
////////	***********		///////////		**********		////////////

////////	***********		///////////		**********		////////////
/*
	APPENDIX A: SUPPORTING FIGURES AND TABLES
Table A1. Balance tests of random treatment assignment 
Table A2. PPE ownership for tomato farmers in Mkushi, Zambia at the baseline
Table A3. Effects of information on WTP for gloves and masks using BDM bid data - Linear projection and Tobit models 
Table A4. Price elasticity of demand estimates by price (N= 505)
Table A5. Endline PPE purchases by treatment assignment
Table A6. Pesticide products and prices used in choice experiment choice sets
Table A7. Intention-to-treat effects of information on changes in choice toxicity across survey rounds from choice experiment data - Alternative toxicity change codes 
Table A8. Revealed demand sub-sample balance tests of random treatment assignment (n=163)
Table A9. Stated choice toxicity market shares by treatment and control group assignment
Table A10. Chi-squared tests of differences in the average partial effects of treatment for changes in choice toxicity presented in Table 2
Table A11. Treatment on treated effects of information on WTP for gloves and masks from Becker-DeGroot-Marschak mechanisms bid data

APPENDIX B: RISK SUBSTITUTION TESTS
Table B1. Average partial effects of knowledge on WTP for gloves and masks

Figure A5: Kernel density plots of price per liter of class Ib and class U insecticides


APPENDIX C: TRIMMED SAMPLE RESULTS
Table C1. Trimmed sample comparison to Table 1. Intention-to-treat effects of information on relative toxicity knowledge and PPE knowledge scores
Table C2. Trimmed sample comparison to Table 2. Intention-to-treat effects of information on changes in choice toxicity across survey rounds from choice experiment data
Table C3. Trimmed sample comparison to Table 3. Average partial effects of information on pesticide choice probabilities and price coefficients from conditional logit estimations of choice experiment data
Table C4. Trimmed sample comparison to Table 4. Intention-to-treat effects of information on revealed pesticide demand at endline
Table C5. Trimmed sample comparison to Table 5. Revealed demand pesticide market shares for different toxicity levels by pest and by survey round
Table C6. Trimmed sample comparison to Table 6. Intention-to-treat effects of information on WTP for gloves and masks from Becker-DeGroot-Marschak mechanisms bid data
Table C7. Trimmed sample comparison to Table 7. Heterogeneous treatment effects of information on WTP for gloves and masks from Becker-DeGroot-Marschak mechanism bid data

*/			
////// //////////////// ////// //////////////// ////// //////////////// ////// //////////////// 
 
////            Table A1.   BALANCE TESTS			 				     ////// ////////////////

////// //////////////// ////// //////////////// ////// //////////////// ////// ////////////////
use "${temp}/_tableSix.dta", clear

// Create dummy variable for primary education (grade 7)
gen primary = 1 if ed>7 & ed!=.
replace primary = 0 if primary==. & ed!=.

sum HHownTom_d HHa0_15 HHa15plus inc_pca AgTrainingHH age primary read female years10 ///
 SalWage Business area_ha e_pesticide  bmask bgloves bboots k_ppe1 Ib II ///
 U AS_15 clinic HORT_all  if insample==1 & mask==1
 
/// Ttests by Treatment group

// Table 1 Output 
foreach outcome in HHownTom_d HHa0_15 HHa15plus inc_pca AgTrainingHH age primary read female years10 ///
	SalWage Business area_ha e_pesticide bmask bgloves bboots k_ppe1 Ib II U clinic AS_15 HORT_all {
	// means
	sum `outcome' if mask==1 & insample==1 
	local means_all = r(mean)
	local sd_all = r(sd)
	
	// econometric output
	reg `outcome' TREAT if insample==1 & mask==1, cluster(EA)
	testparm TREAT
	local pvalue=r(p)
	
	eststo tableA1_`outcome', addscalars(means_all `means_all' sd_all `sd_all' pvalue `pvalue')
}

// Exporting the regression results
xml_tab tableA1_* , save($output/Table_A1.xls) sd2 replace stats(N pvalue means_all sd_all) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(Table A1 Balance Results) 

/// note: balance test for prices paid per L of insecticide found in section on Figure A4 below
///  circa 2035

////////////////////////////////////////////////////////

			// 	Appendix Table A2. PPE OWNERSHIP AT BASELINE	///
////////////////////////////////////////////////////////
use "${temp}/_tableSix.dta", clear


//  		PPE  Ownership at baseline			///
recode bgloves bmask bboots bsuit bgoggles (2=0)
gen bfullppe = (bgloves==1&bmask==1&bboots==1&bsuit==1&bgoggles==1)
gen bppecount = bgloves+bmask+bboots+bsuit + bgoggles

//  		PPE  USE at baseline			///
// create variables for 'always' use of PPE items while mixing AND applying pesticides
gen boots_use = Boots_MIX01==1&Boots_APPLY01==1
gen gloves_use = Gloves_MIX02==1&Gloves_APPLY02==1
gen mask_use = Mask_MIX03==1&Mask_APPLY03==1
gen gog_use = Gog_MIX04==1&Gog_APPLY04==1
gen over_use = Over_MIX05==1&Over_APPLY05==1
gen full_use = boots_use==1&gloves_use==1&mask_use==1&gog_use==1&over_use==1
gen ppeuse = gloves_use+mask_use+boots_use+over_use+gog_use

tabstat bfullppe bgloves bmask bboots bsuit bgoggles if insample==1 & mask==1, st(mean) save
matrix _own = r(StatTotal)
matrix list _own

tabstat full_use gloves_use mask_use boots_use over_use gog_use if insample==1 & mask==1, st(mean) save
matrix _use = r(StatTotal)
matrix list _use

tabstat bppecount ppeuse if insample==1 & mask==1, st(median mean) save
matrix _PPE2 = r(StatTotal)
matrix list _PPE2

matrix own = _own'
matrix use = _use'

putexcel set "$output/Table_A2.xlsx", replace
putexcel B1=("Own") C1=("Always use") A2 = ("Full PPE") A3=("Gloves") A4=("Dust mask") A5=("Boots") A6=("Worksuit") A7=("Goggles") A9=("Median") A10=("Mean")
putexcel B2=matrix(own) C2=matrix(use), nformat(number_d2)
putexcel B9=matrix(_PPE2), nformat(number_d2)

////////////////////////////////////////////////////////

			// 	Appendix Table A3. ITT - tobit and LPM	///

////////////////////////////////////////////////////////
use "${temp}/_tableSix.dta", clear

//		LINEAR PROBABILITY MODEL

clonevar _mask = mask
recode _mask (0= 2)
forvalues x = 1(1)2 {
	// random inference tests
	ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(111) :  areg b_pos TREAT if _mask==`x' & insample==1, vce(cluster EA) a(block)
	matrix _rit = r(p)
	local ritest_p = _rit[1,1]

	// means
	sum bid if _mask==`x' & insample==1 & TREAT==0	 
	local means_all = r(mean)

	// econometric output
	areg b_pos TREAT if _mask==`x' & insample==1, vce(cluster EA) a(block)
	testparm TREAT
	local pvalue_treat=r(p)
		
	boottest TREAT, nograph
	local pvalue_wcb_treat = r(p)
	
		
	eststo tableA3_LPM_mask`x'_1, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_treat `pvalue_treat' ///
		pvalue_wcb_treat `pvalue_wcb_treat')	

		// with covariate controls
	ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(111) :  areg b_pos TREAT Bus read HORT_all if _mask==`x' & insample==1, vce(cluster EA) a(block)
	matrix _rit = r(p)
	local ritest_p = _rit[1,1]

	// means
	sum bid if _mask==`x' & insample==1 & TREAT==0	 
	local means_all = r(mean)

	// econometric output
	areg b_pos TREAT Bus read HORT_all if _mask==`x' & insample==1, vce(cluster EA) a(block)
	testparm TREAT
	local pvalue_treat=r(p)
		
	boottest TREAT, nograph
	local pvalue_wcb_treat = r(p)
	
		
	eststo tableA3_LPM_mask`x'_2, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_treat `pvalue_treat' ///
		pvalue_wcb_treat `pvalue_wcb_treat')	

	
		}
	

// Exporting the regression results
xml_tab tableA3_LPM* , save($output/Table_A3.xls) sd2 replace sheet(LPM) stats(N r2 pvalue_treat pvalue_wcb_treat ritest_p means_all)format(SCLR3 NCLR3) ///
	keep(TREATMENT) title(Table A3 LPM Regression Results) 


	//		TOBIT
forvalues x = 0(1)1 {
	// random inference tests

	// means
	sum bid if mask==`x' & insample==1 & TREAT==0	 
	local means_tob1_`x' = r(mean)

	// econometric output
	tobit bid TREAT i.block if mask==`x' & insample==1, vce(cluster EA) ll(0)
	margins, dydx(TREAT) predict(ystar(0,.))
	matrix mat1_`x' = r(table)
	mat list mat1_`x'
	pause
	local N_tob1_`x' = e(N) 
	local dpdx_tob1_`x' = mat1_`x'[1,1]
	local se_tob1_`x' = mat1_`x'[2,1]
	local pvalue_tob1_`x' = mat1_`x'[4,1]
		
	// with covariate controls
		// means
	quietly sum bid if mask==`x' & insample==1 & TREAT==0	 
	local means_tob2_`x' = r(mean)

	// econometric output
	tobit bid TREAT Bus read HORT_all i.block if mask==`x' & insample==1, vce(cluster EA) ll(0)
	margins, dydx(TREAT) predict(ystar(0,.))
	matrix mat2_`x' = r(table)
	local N_tob2_`x' = e(N) 
	local dpdx_tob2_`x' = mat2_`x'[1,1]
	local se_tob2_`x' = mat2_`x'[2,1]
	local pvalue_tob2_`x' = mat2_`x'[4,1]
		
		}

putexcel set "$output\Table_A3_Tobit.xls", sheet("Tobit") replace
putexcel B1=("Tobit_Mask1") C1=("Tobit_Mask2") D1=("Tobit_Gloves1") E1=("Tobit_Gloves2") ///
	A2=("TREAT") A3=("se") A4=("pvalue") A5=("N") 
putexcel B2=(`dpdx_tob1_1') C2=(`dpdx_tob2_1') D2=(`dpdx_tob1_0') E2=(`dpdx_tob2_0'), nformat(number_d3)
putexcel B3=(`se_tob1_1') C3=(`se_tob2_1') D3=(`se_tob1_0') E3=(`se_tob2_0'), nformat(number_d3)
putexcel B4=(`pvalue_tob1_1') C4=(`pvalue_tob2_1') D4=(`pvalue_tob1_0') E4=(`pvalue_tob2_0'), nformat(number_d3)
putexcel B5=(`N_tob1_1') C5=(`N_tob2_1') D5=(`N_tob1_0') E5=(`N_tob2_0'), nformat(number_d3)

////////////////////////////////////////////////////////

			// 	Appendix Table A4. Computed in Excel     ///

////////////////////////////////////////////////////////

////////////////////////////////////////////////////////

			// 	Appendix Table A5. Endline PPE purchases	///

////////////////////////////////////////////////////////


//  Create variables for PURCHASED PPE items (price>0)
gen pgloves = (PPE04==1&PPE06!=0) if !missing(PPE04) 
gen pmask = (PPE07==1 & PPE09!=0) if !missing(PPE07)
gen pboots = (PPE01==1 & PPE03!=0) if !missing(PPE01)
gen psuit = (PPE13==1 & PPE15!=0) if !missing(PPE13)
gen pgoggles = (PPE10==1 & PPE12!=0) if !missing(PPE10)

gen pPPEc = pgloves + pmask + pboots + psuit + pgoggles

// create dummy for at least 1 ppe item purchased at endline
gen pPPE = (pgloves==1| pmask==1| pboots==1| psuit==1| pgoggles==1)

///			TABLES
forvalues x = 0(1)1 {
tabstat pgloves pmask pboots pgoggles psuit pPPE pPPEc if insample==1 & mask==1 & TREAT==`x', st(mean sum) save
matrix _purch`x' = r(StatTotal)
matrix purch`x' = _purch`x''
}

tabstat pgloves pmask pboots pgoggles psuit pPPE pPPEc if insample==1 & mask==1 , st(mean sum) save
matrix _purchAll = r(StatTotal)
matrix purchAll = _purchAll'



putexcel set "$output/Table_A5.xlsx", replace
putexcel B1=("All_Share") C1=("All_n") D1=("Treat_Share") E1=("Treat_n") F1=("Control_Share") G1=("Control_n") ///
	A2 = ("Gloves") A3=("Mask") A4=("Boots") A5=("Goggles") A6=("Worksuit") A7=("AnyPPE") A8=("Mean")
putexcel B2=matrix(purchAll) D2=matrix(purch1) F2=matrix(purch0), nformat(number_d2)
putexcel C8=("-") E8=("-") G8=("-"), nformat(number_d2)




////////////////////////////////////////////////////////

			// 	Appendix Table A7. ITT of info on choice toxicity ///
					// alternative toxicity change codes
			
////////////////////////////////////////////////////////

// NEMATODES EXPERIMENT CHOICES
use "${temp}/_tableTwoNem.dta", clear
//// CHANGE IN TOX VARIABLE  - First-difference estimation
recode Rd2_Tox Rd1_Tox (3=2)
recode Rd2_Tox Rd1_Tox (4=3)

// create a change in toxicity score (=1 if decrease, =2 if no change, =3 if increase)
gen change_tox2 = (Rd2_Tox - Rd1_Tox)

// Table A7 Output 
// Regression 1 - LPM

// random inference tests
ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(321) :  reg change_tox2 TREAT i.block if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
matrix LPM_rit = r(p)
local ritest_p = LPM_rit[1,1]

// means
sum Rd1_Tox if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1
local means_all = r(mean)

// econometric output
reg change_tox2 TREAT i.block if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
testparm TREAT
local pvalue_all=r(p)
boottest TREAT, nograph
local pvalue_wcb = r(p)

eststo tableA7_LPM, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')

// // Exporting the regression results
xml_tab tableA7_LPM , save($output/Table_A7.xls) sheet(LPM_Nem) sd2 replace stats(ritest_p means_all pvalue_all pvalue_wcb) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(Table A7 LPM Nematode Results) 



// ORDERED PROBIT	
oprobit change_tox2 TREAT if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
eststo tableA7_NemOP
	
foreach delta in 2 1 {
estimates restore tableA7_NemOP
margins, dydx(TREAT) predict(outcome(-`delta')) post
eststo _tableA7_op_neg`delta'
}
foreach delta in 0 1 2 {
estimates restore tableA7_NemOP
margins, dydx(TREAT) predict(outcome(`delta')) post
eststo _tableA7_op_`delta'
}

xml_tab _tableA7_op_* , save($output/Table_A7.xls) append sheet(OP_Nem) sd2 ///
	format(SCLR3 NCLR3) title(Table A7 OP Nematode Results) 
	
eststo clear
	
	 ////////////////////////
			
			/////////   Table A7 BOLLWORMS ////////////

// BOLLWORMS EXPERIMENT CHOICES
use "${temp}/_tableTwoBw.dta", clear
//// CHANGE IN TOX VARIABLE  - First-difference estimation
recode Rd2_Tox Rd1_Tox (3=2)
recode Rd2_Tox Rd1_Tox (4=3)

// create a change in toxicity score (=1 if decrease, =2 if no change, =3 if increase)
gen change_tox2 = (Rd2_Tox - Rd1_Tox)
			
			
// Regression 1 - LPM

// random inference tests
ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(321) :  reg change_tox2 TREAT i.block if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
matrix LPM_rit = r(p)
local ritest_p = LPM_rit[1,1]

// means
sum Rd1_Tox if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1
local means_all = r(mean)

// econometric output
reg change_tox2 TREAT i.block if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
testparm TREAT
local pvalue_all=r(p)
boottest TREAT, nograph
local pvalue_wcb = r(p)

eststo tableA7_LPM, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')

// // Exporting the regression results
xml_tab tableA7_LPM , save($output/Table_A7.xls) sheet(LPM_Bollworm) sd2 append stats(ritest_p means_all pvalue_all pvalue_wcb) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(Table A7 LPM Bollworm Results) 



// ORDERED PROBIT	
oprobit change_tox2 TREAT if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & rd==1, vce(cluster EA)
eststo tableA7_BwOP
	
foreach delta in 2 1 {
estimates restore tableA7_BwOP
margins, dydx(TREAT) predict(outcome(-`delta')) post
eststo _tableA7_op_neg`delta'
}
foreach delta in 0 1 2 {
estimates restore tableA7_BwOP
margins, dydx(TREAT) predict(outcome(`delta')) post
eststo _tableA7_op_`delta'
}

xml_tab _tableA7_op_* , save($output/Table_A7.xls) append sheet(OP_Bollworm) sd2 ///
	format(SCLR3 NCLR3) title(Table A7 OP Bollworm Results) 
	
eststo clear			
			
			
////////////////////////////////////////////////////////

			// 	Table A8. Revealed demand sub-sample balance tests of random treatment assignment (n=163) ///
			
////////////////////////////////////////////////////////
			
////////////////////////////////////////////////////////

			// 	BALANCE TESTS FOR REV DEMAND SUB-SAMPLE	///

////////////////////////////////////////////////////////

use "${temp}/_tableFive.dta", clear
joinby FMR using "${temp}/_tableSix.dta", _merge(_111)

keep if bw!=0|nem!=0

// Create dummy variable for primary education (completed grade 7)
gen primary = 1 if ed>7 & ed!=.
replace primary = 0 if primary==. & ed!=.

/// Ttests by Treatment group

// Table A8 Output 
foreach outcome in HHownTom_d HHa0_15 HHa15plus inc_pca AgTrainingHH age primary read female years10 ///
	SalWage Business area_ha e_pesticide bmask bgloves bboots k_ppe1 Ib II U clinic AS_15 HORT_all {
	// means
	sum `outcome' if insample==1 & mask==1
	local means_all = r(mean)
	local sd_all = r(sd)
	
	// econometric output
	reg `outcome' TREAT if mask==1, cluster(EA)
	testparm TREAT
	local pvalue=r(p)
	
	eststo tableA8_`outcome', addscalars(means_all `means_all' sd_all `sd_all' pvalue `pvalue')
}

// Exporting the regression results
xml_tab tableA8_* , save($output/Table_A8.xls) sd2 replace stats(N pvalue means_all sd_all) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(Table A8 Balance Results) 


// price paid per liter
//	results circa 2040 below
	
////////////////////////////////////////////////////////

			// 	Appendix Table A9. Stated choice toxicity market shares ///
			
////////////////////////////////////////////////////////
foreach type in Bw Nem {
	forvalues i = 0(1)1 {
		use "${temp}/_tableTwo`type'.dta", clear
			// create inverted toxScore for table
			recode ToxScore (1 = 4) (3 = 2) (4 = 1)
			tab ToxScore TREAT if rd==`i' & wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3, chi2 exact matcell(cell`type'_rd`i') matrow(toxScore) matcol(treat)
			local chi2_`type'_rd`i'=r(chi2)
			local p_`type'_rd`i'=r(p)
			}
		}

putexcel set "$output/Table_A9.xlsx", replace
putexcel A1=("BOLLWORM") A4=matrix(toxScore) B2=("BASELINE") B3=("Control") C3=("Treat") B4=matrix(cellBw_rd0) ///
	E2=("ENDLINE") E3=("Control") F3=("Treat") E4=matrix(cellBw_rd1)
putexcel A7=("ChiSquare") B7=("`chi2_Bw_rd0'") C7=("`p_Bw_rd0'") E7=("`chi2_Bw_rd1'") F7=("`p_Bw_rd1'")

putexcel A9=("NEMATODE") A12=matrix(toxScore) B10=("BASELINE") B11=("Control") C11=("Treat") B12=matrix(cellNem_rd0) ///
	E10=("ENDLINE") E11=("Control") F11=("Treat") E12=matrix(cellNem_rd1)
putexcel A15=("ChiSquare") B15=("`chi2_Nem_rd0'") C15=("`p_Nem_rd0'") E15=("`chi2_Nem_rd1'") F15=("`p_Nem_rd1'")
		
	
////////////////////////////////////////////////////////

			// 	Appendix Table A10. Chi-squared tests of differences in the average partial effects of treatment for changes in choice toxicity presented in Table 2 ///
			
////////////////////////////////////////////////////////
			

use "${temp}/_tableTwoBw.dta", clear
			
foreach depvar in change_tox {
	oprobit `depvar' i.TREAT if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3, vce(cluster EA)
	margins, dydx(*) post
	
	forvalues round = 1/6 {
		local j = `round' + 1
		di in r `j'
		forvalues x = `j'/7 {
			test _b[1.TREATMENT:`round'._predict] = _b[1.TREATMENT:`x'._predict]
			local pvalue_`round'_`x' = r(p)
			di in r "pvalue_`round'_`x'" " is " "`pvalue_`round'_`x''"
			
			}
		if `round' == 1 {
			matrix compare_`round' = `pvalue_`round'_2', `pvalue_`round'_3', `pvalue_`round'_4', `pvalue_`round'_5', `pvalue_`round'_6', `pvalue_`round'_7'
		}
		
		if `round' == 2 {
			matrix compare_`round' = ., `pvalue_`round'_3', `pvalue_`round'_4', `pvalue_`round'_5', `pvalue_`round'_6', `pvalue_`round'_7'
		}

		if `round' == 3 {
			matrix compare_`round' = ., ., `pvalue_`round'_4', `pvalue_`round'_5', `pvalue_`round'_6', `pvalue_`round'_7'
		}

		if `round' == 4 {
			matrix compare_`round' = ., ., ., `pvalue_`round'_5', `pvalue_`round'_6', `pvalue_`round'_7'
		}

		if `round' == 5 {
			matrix compare_`round' = ., ., ., ., `pvalue_`round'_6', `pvalue_`round'_7'
		}
		
		if `round' == 6 {
			matrix compare_`round' = ., ., ., ., ., `pvalue_`round'_7'
		}
		
	}
	
	matrix test = (compare_1 \ compare_2 \ compare_3 \ compare_4 \ compare_5 \ compare_6) 
	matrix rownames test = coef_1 coef_2 coef_3 coef_4 coef_5 coef_6
	matrix colnames test = coef_2 coef_3 coef_4 coef_5 coef_6 coef_7
	mat list test
}
xml_tab test, save(${output}\Table_A10.xls) title (Chi Squared Tests Between Coefficients) replace sheet(Bw) format(SCLR3 NCLR3)
			
use "${temp}/_tableTwoNem.dta", clear
foreach depvar in change_tox {
	oprobit `depvar' i.TREAT if wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3, vce(cluster EA)
	margins, dydx(*) post
	
	forvalues round = 1/6 {
		local j = `round' + 1
		di in r `j'
		forvalues x = `j'/7 {
			test _b[1.TREATMENT:`round'._predict] = _b[1.TREATMENT:`x'._predict]
			local pvalue_`round'_`x' = r(p)
			di in r "pvalue_`round'_`x'" " is " "`pvalue_`round'_`x''"
			
			}
		if `round' == 1 {
			matrix compare_`round' = `pvalue_`round'_2', `pvalue_`round'_3', `pvalue_`round'_4', `pvalue_`round'_5', `pvalue_`round'_6', `pvalue_`round'_7'
		}
		
		if `round' == 2 {
			matrix compare_`round' = ., `pvalue_`round'_3', `pvalue_`round'_4', `pvalue_`round'_5', `pvalue_`round'_6', `pvalue_`round'_7'
		}

		if `round' == 3 {
			matrix compare_`round' = ., ., `pvalue_`round'_4', `pvalue_`round'_5', `pvalue_`round'_6', `pvalue_`round'_7'
		}

		if `round' == 4 {
			matrix compare_`round' = ., ., ., `pvalue_`round'_5', `pvalue_`round'_6', `pvalue_`round'_7'
		}

		if `round' == 5 {
			matrix compare_`round' = ., ., ., ., `pvalue_`round'_6', `pvalue_`round'_7'
		}
		
		if `round' == 6 {
			matrix compare_`round' = ., ., ., ., ., `pvalue_`round'_7'
		}
		
	}
	
	matrix test = (compare_1 \ compare_2 \ compare_3 \ compare_4 \ compare_5 \ compare_6) 
	matrix rownames test = coef_1 coef_2 coef_3 coef_4 coef_5 coef_6
	matrix colnames test = coef_2 coef_3 coef_4 coef_5 coef_6 coef_7
	mat list test
}
xml_tab test, save(${output}\Table_A10.xls) title (Chi Squared Tests Between Coefficients) append sheet(Nem) format(SCLR3 NCLR3)


////////////////////////////////////////////////////////

			// 	Appendix Table A11. Treatment on treated effects of information on WTP for gloves and masks
			
////////////////////////////////////////////////////////
	

use "${temp}/_tableSix.dta", clear

quietly sum reached_any if insample==1 &  mask==1 & TREAT==1	
local _Training_or_Letter = r(mean)
quietly sum training if insample==1 &  mask==1 & TREAT==1	
local _training = r(mean)
quietly sum letter if insample==1 &  mask==1 & TREAT==1	
local _letter = r(mean)

quietly sum bid if insample==1 &  mask==1 & TREAT==0
local mask_mean = r(mean)
quietly sum bid if insample==1 &  mask==0 & TREAT==0
local glove_mean = r(mean)

	
// 			LPM
// Mask
// 		no covariates
reg bid reached_any i.block if mask==1 & insample==1, vce(cluster EA)
outreg2 using "$output/Table_A11.xls" , replace dec(3) nocons keep(reached_any) addstat(mask_mean, `mask_mean', ///
	glove_mean, `glove_mean', Training or Letter, `_Training_or_Letter', Training, `_training', Letter, `_letter')
// 		unbalanced covariates`
reg bid reached_any Bus read HORT_all i.block if mask==1 & insample==1, vce(cluster EA)
outreg2 using "$output/Table_A11.xls" , dec(3) nocons keep(reached_any)

foreach type in training letter {
	// 		no covariates
	areg bid `type' if mask==1 & insample==1, vce(cluster EA) a(block)
	outreg2 using "$output/Table_A11.xls" , dec(3) nocons keep(`type')

	// 		unbalanced covariates
	areg bid `type' Bus read HORT_all if mask==1 & insample==1, vce(cluster EA) a(block)
	outreg2 using "$output/Table_A11.xls" , dec(3) nocons keep(`type')
}

foreach type in reached_any training letter {
	// 		no covariates
	areg bid `type' if mask==0 & insample==1, vce(cluster EA) a(block)
	outreg2 using "$output/Table_A11.xls" , dec(3) nocons keep(`type')

	// 		unbalanced covariates
	areg bid `type' Bus read HORT_all if mask==0 & insample==1, vce(cluster EA) a(block)
	outreg2 using "$output/Table_A11.xls" , dec(3) nocons keep(`type')
}


	
////////////////////////////////////////////////////////

			// 	/// Appendix Table B1. EXOGENOUS KNOWLEDGE EFFECTS /////////
			
////////////////////////////////////////////////////////
		
			
/////		margins by interactions
// 	OLS

use "${temp}/_tableSix.dta", clear

clonevar _mask = mask
recode _mask (0= 2)
foreach outcome in bid b_pos {
	forvalues x = 1(1)2 {
	
		/// add mean knowledge and bids
	sum k_ppe if _mask==`x' & insample==1
	local mean_k_ppe = r(mean)
	sum kIbU if mask ==`x' & insample==1
	local mean_kIbU = r(mean)
	sum bid if mask==`x' & insample==1
	local mean_mask`x'= r(mean)
	
	areg `outcome' c.k_ppe##c.kIbU Bus read HORT_all if _mask==`x' & insample==1, cluster(EA) a(block)
	eststo temp_`outcome'_`x'

	// overall margins
	margins, dydx(k_ppe) post
	eststo B1_all1_`outcome'_mask`x', addscalars(mean_k_ppe `mean_k_ppe' mean_k_tox `mean_kIbU' ///
	mean_bid_mask`x' `mean_mask`x'')
	estimates restore temp_`outcome'_`x'
	margins, dydx(k_ppe) at(kIbU=(0(1)1)) vsquish post
	eststo B1_kppe_`outcome'_mask`x'
	estimates restore temp_`outcome'_`x'
		// overall margins
	margins, dydx(kIbU) post
	eststo B1_all2_`outcome'_mask`x'
	estimates restore temp_`outcome'_`x'
	margins, dydx(kIbU) at(k_ppe=(0(1)5)) vsquish post
	eststo B1_kIbU_`outcome'_mask`x'	
}
}

xml_tab B1* , save($output/Table_B1.xls) replace  sd2 stats(N) format(SCLR3 NCLR3) title(Table B1 APE of knowledge on WTP for PPE) 

forvalues i =1(1)2 {
	sum k_ppe if _mask==`i' & insample==1
	local mean_k_ppe`i' = r(mean)
	sum kIbU if _mask ==`i' & insample==1
	local mean_kIbU`i' = r(mean)
	sum bid if _mask==`i' & insample==1
	local mean_bid`i'= r(mean)
	}

putexcel set "$output/Table_B1_means.xlsx", replace
putexcel A1=("Mean K_ppe") A2=("Mean K_tox") A3=("Mean bid - Mask") A4 = ("Mean bid - Gloves")
putexcel B1=("`mean_k_ppe1'") B2=("`mean_kIbU1'") B3=("`mean_bid1'") B4=("`mean_bid2'")

////// //////////////// ////// //////////////// ////// //////////////// ////// //////////////// 
 
////            Figure A5.   Price distributions		 				     ////// ////////////////

////// //////////////// ////// //////////////// ////// //////////////// ////// ////////////////

// TOXICITY OF PESTICIDES USED
use "${Round2}\PesticidePurchases_Codes.dta", clear

joinby FMR using "${temp}\FMRcharacteristics.dta", unmatched(both) _merge(_merge)
// 239 farmers did not purchase pesticides since RD1

gen insample = (replace!=1)
keep if insample==1
drop if FMR==.

joinby CODE using "${temp}\WHO_codes.dta", unmatched(both) _merge(_1)
tab type
tab who

tab TREATMENT who

gen insL = PPurch02/1000 if (type==2|type==4) & PPurch03==3
replace insL = PPurch02 if (type==2|type==4) & PPurch03==4
gen fungKg = PPurch02/1000 if type==1 & PPurch03==1
replace fungKg = PPurch02 if type==1 & PPurch03==2

/// clean
replace PPurch03=1 if fungKg>15 & fungKg!=.
replace PPurch03=2 if fungKg<.1 & PPurch04>30
replace PPurch03=3 if insL>100 & insL!=.
replace PPurch02=1 if insL==91
replace PPurch03=4 if PPurch03==3 & PPurch02<10 &PPurch04>10 & insL<0.05

drop insL fungKg
gen insL = PPurch02/1000 if (type==2|type==4) & PPurch03==3
replace insL = PPurch02 if (type==2|type==4) & PPurch03==4
gen fungKg = PPurch02/1000 if type==1 & PPurch03==1
replace fungKg = PPurch02 if type==1 & PPurch03==2

/// make a restriction on volume
gen insLPrice = PPurch04/insL
gen fungKgPrice = PPurch04/fungKg

list typ PPurch02-PPurch04 insL insLPrice if insLPrice>300 & insLPrice!=.
	// all very small volumes
list typ PPurch02-PPurch04 fungKg fungKgPrice if fungKgPrice>160 & fungKgPrice!=.	
	// all very small volumes
recode insLPrice fungKgPrice (0 = .)


bysort who: sum insLPrice if insL>.1, det	
bysort who: sum fungKgPrice if fungKg>.05, det	
	
// visualize distributions	
	
twoway kdensity insLPrice if insL>.1 & who==1, bw(30) || kdensity insLPrice if insL>.1 & who==4, bw(30) 

// separately to put into excel
kdensity insLPrice if insL>.1 & who==1, bw(30) gen(ins1b_x ins1b_y)
kdensity insLPrice if insL>.1 & who==4, bw(30) gen(insU_x insU_y) 

bysort who: sum insLPrice if insL>.1, det


// balance tests for balance table
ttest insLPrice, by(TREAT) unequal

// balance tests on revealed demand sub sample
joinby FMR using "${temp}/_tableFive.dta", _merge(_m1)
keep if bw!=0|nem!=0


/// balance tests for prices for table A1
ttest insLPrice, by(TREAT) unequal


/*
APPENDIX C: TRIMMED SAMPLE RESULTS
Table C1. Trimmed sample comparison to Table 1. Intention-to-treat effects of information on relative toxicity knowledge and PPE knowledge scores
Table C2. Trimmed sample comparison to Table 2. Intention-to-treat effects of information on changes in choice toxicity across survey rounds from choice experiment data
Table C3. Trimmed sample comparison to Table 3. Average partial effects of information on pesticide choice probabilities and price coefficients from conditional logit estimations of choice experiment data
Table C4. Trimmed sample comparison to Table 4. Intention-to-treat effects of information on revealed pesticide demand at endline
Table C5. Trimmed sample comparison to Table 5. Revealed demand pesticide market shares for different toxicity levels by pest and by survey round
Table C6. Trimmed sample comparison to Table 6. Intention-to-treat effects of information on WTP for gloves and masks from Becker-DeGroot-Marschak mechanisms bid data
Table C7. Trimmed sample comparison to Table 7. Heterogeneous treatment effects of information on WTP for gloves and masks from Becker-DeGroot-Marschak mechanism bid data

*/
		
			
			
////////////////////////////////////////////////////////

			// 	Trimmed Sample Robustness Checks	///

////////////////////////////////////////////////////////


/// TABLE C1

use "${temp}/_tableOne.dta", clear
joinby FMR using "${temp}\trim_BDM.dta", unmatched(both) _merge(_16)
gen trimsample = (TRIM!=1 & replace!=1 & trim_BDMm !=1 & trim_BDMg!=1 & block!=2 & block!=15)

///////  TABLE C1 ROBUSTNESS CHECK - TRIMMED SAMPLE
// Table C1 Output 
foreach outcome in kIbU k_ppe {
	// Regression 1
	
	// random inference tests
	ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(321) :  reg `outcome' TREAT i.block if trimsample==1, vce(cluster EA)
	matrix `outcome'_rit = r(p)
	local ritest_p = `outcome'_rit[1,1]

	// means
	reg `outcome' i.TREAT if trimsample == 1 & TREAT == 0
	matrix `outcome'_mean = e(b)
	local means_all = `outcome'_mean[1,2]
	
	// econometric output
	reg `outcome' TREAT i.block if trimsample==1, vce(cluster EA)
	testparm TREAT
	local pvalue_all=r(p)
	boottest TREAT, nograph
	local pvalue_wcb = r(p)
	
	eststo tableC1_`outcome'_reg1, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')
	
	// Regression 2
	
	// random inference tests
	ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(321) :  reg `outcome' TREAT Bus read HORT_all i.block if trimsample==1, vce(cluster EA)
	matrix `outcome'_rit = r(p)
	local ritest_p = `outcome'_rit[1,1]

	// means
	reg `outcome' i.TREAT if trimsample == 1 & TREAT == 0
	matrix `outcome'_mean = e(b)
	local means_all = `outcome'_mean[1,2]
	
	// econometric output
	reg `outcome' TREAT Bus read HORT_all i.block if trimsample==1, vce(cluster EA)
	testparm TREAT
	local pvalue_all=r(p)
	boottest TREAT, nograph
	local pvalue_wcb = r(p)
	
	eststo tableC1_`outcome'_reg2, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')
	
}

// Exporting the regression results
xml_tab tableC1_* , save($output/Table_C1.xls) sd2 replace stats(N r2 pvalue_all pvalue_wcb ritest_p means_all) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(TRIMMED SAMPLE: Table 1 Regression Results) 

		
////// //////////////// ////// //////////////// ////// //////////////// ////// //////////////// 

////// //////////////// ////// //////////////// ////// //////////////// ////// //////////////// 

		
///////  TABLE C2 ROBUSTNESS CHECK - TRIMMED SAMPLE

use "${temp}/_tableTwoNem.dta", clear
joinby FMR using "${temp}\trim_BDM.dta", unmatched(both) _merge(_16)
gen trimsample = (TRIM!=1 & replace!=1 & trim_BDMm !=1 & trim_BDMg!=1 & block!=2 & block!=15)


			/////////   TABLE 2  NEMATODES ////////////
			
// Table 2 Output 
// Regression 1 - LPM

// random inference tests
ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(321) :  reg change_tox TREAT i.block if block!=2 & block!=15 & wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & TRIM!=1  & rd==1, vce(cluster EA)
matrix LPM_rit = r(p)
local ritest_p = LPM_rit[1,1]

// means
sum Rd1_Tox if block!=2 & block!=15 & wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & TRIM!=1  & rd==1
local means_all = r(mean)

// econometric output
reg change_tox TREAT i.block if block!=2 & block!=15 & wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & TRIM!=1  & rd==1, vce(cluster EA)
testparm TREAT
local pvalue_all=r(p)
boottest TREAT, nograph
local pvalue_wcb = r(p)

eststo tableC2_LPM, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')

// // Exporting the regression results
xml_tab tableC2_LPM , save($output/Table_C2.xls) sheet(LPM_Nem) sd2 replace stats(ritest_p means_all pvalue_all pvalue_wcb) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(TRIMMED SAMPLE - Table C2 LPM Nematode Results) stats(N)



// ORDERED PROBIT	
oprobit change_tox TREAT if block!=2 & block!=15 & wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & TRIM!=1  & rd==1, vce(cluster EA)
eststo tableC2_NemOP
	
foreach delta in 3 2 1 {
estimates restore tableC2_NemOP
margins, dydx(TREAT) predict(outcome(-`delta')) post
eststo _tableC2_op_neg`delta'
}
foreach delta in 0 1 2 3 {
estimates restore tableC2_NemOP
margins, dydx(TREAT) predict(outcome(`delta')) post
eststo _tableC2_op_`delta'
}

xml_tab _tableC2_op_* , save($output/Table_C2.xls) append sheet(OP_Nem) sd2 ///
	format(SCLR3 NCLR3) title(TRIMMED SAMPLE - Table C2 OP Nematode Results) 
	
eststo clear

			/////////   TABLE 2 BOLLWORMS ////////////
use "${temp}/_tableTwoBw.dta", clear
joinby FMR using "${temp}\trim_BDM.dta", unmatched(both) _merge(_16)		
// Regression 1 - LPM

// random inference tests
ritest TREAT _b[TREAT], cluster(EA) reps(1000) seed(321) :  reg change_tox TREAT i.block if block!=2 & block!=15 & wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & TRIM!=1  & rd==1, vce(cluster EA)
matrix LPM_rit = r(p)
local ritest_p = LPM_rit[1,1]

// means
sum Rd1_Tox if block!=2 & block!=15 & wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & TRIM!=1  & rd==1
local means_all = r(mean)

// econometric output
reg change_tox TREAT i.block if block!=2 & block!=15 & wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & TRIM!=1  & rd==1, vce(cluster EA)
testparm TREAT
local pvalue_all=r(p)
boottest TREAT, nograph
local pvalue_wcb = r(p)

eststo tableC2_LPM, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')

// // Exporting the regression results
xml_tab tableC2_LPM , save($output/Table_C2.xls) sheet(LPM_Bollworm) sd2 append stats(ritest_p means_all pvalue_all pvalue_wcb) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(TRIMMED SAMPLE - Table C2 LPM Bollworm Results) 



// ORDERED PROBIT	
oprobit change_tox TREAT if block!=2 & block!=15 & wrong_price!=1 & replacement!=1 & wrong_block!=1 & _1==3 & TRIM!=1  & rd==1, vce(cluster EA)
eststo tableC2_BwOP
	
foreach delta in 3 2 1 {
estimates restore tableC2_BwOP
margins, dydx(TREAT) predict(outcome(-`delta')) post
eststo _tableC2_op_neg`delta'
}
foreach delta in 0 1 2 3 {
estimates restore tableC2_BwOP
margins, dydx(TREAT) predict(outcome(`delta')) post
eststo _tableC2_op_`delta'
}

xml_tab _tableC2_op_* , save($output/Table_C2.xls) append sheet(OP_Bollworm) sd2 ///
	format(SCLR3 NCLR3) title(TRIMMED SAMPLE - Table C2 OP Bollworm Results) 
	
eststo clear	

////// //////////////// ////// //////////////// ////// //////////////// ////// //////////////// 

////// //////////////// ////// //////////////// ////// //////////////// ////// //////////////// 
		

///////  TABLE C3 ROBUSTNESS CHECK - TRIMMED SAMPLE
///Nematodes
use "${temp}/_tableThreeNem.dta", clear
joinby FMR using "${temp}\FMRcharacteristics.dta", unmatched(both) _merge(_3)

/// RESULTS 
/// BASELINE ESTIMATION
asclogit choice2 price Tp if block!=2 & block!=15 & round==0 & out==0 , case(id) alt(alternative) casevars(TREATMENT) basealternative(2) vce(cluster EA)
quietly estat mfx, varlist(TREAT)
eststo margins
foreach x in 1 2 3  {
	matrix mat_`x' = r(_`x')
	local dpdx`x' = mat_`x'[1,1]
	local se`x' = mat_`x'[1,2]
	local pvalue`x' = mat_`x'[1,4]
}

quietly asclogit choice2 price Tp if block!=2 & block!=15 & round==0 & out==0 , case(id) alt(alternative) casevars(TREATMENT) basealternative(2) vce(cluster EA)
eststo tableC3_Nem_base, addscalars(Umet_dpdx `dpdx1' Umet_se `se1' Umet_p `pvalue1' Orizon_dpdx `dpdx2' Orizon_se `se2' Orizon_p `pvalue2' BioNematon_dpdx `dpdx3' BioNematon_se `se3' BioNematon_p `pvalue3')


/// ENDLINE ESTIMATION
quietly asclogit choice2 price Tp if block!=2 & block!=15 & round==1 & out==0 , case(id) alt(alternative) casevars(TREATMENT) basealternative(2) vce(cluster EA)
quietly estat mfx, varlist(TREAT)
eststo margins_end
foreach x in 1 2 3 4 5 6 {
	matrix mat_`x' = r(_`x')
	local dpdx`x' = mat_`x'[1,1]
	local se`x' = mat_`x'[1,2]
	local pvalue`x' = mat_`x'[1,4]
}

quietly asclogit choice2 price Tp if block!=2 & block!=15 & round==1 & out==0 , case(id) alt(alternative) casevars(TREATMENT) basealternative(2) vce(cluster EA) 
eststo tableC3_Nem_end, addscalars(Umet_dpdx `dpdx1' Umet_se `se1' Umet_p `pvalue1' Orizon_dpdx `dpdx2' Orizon_se `se2' Orizon_p `pvalue2' BioNematon_dpdx `dpdx3' BioNematon_se `se3' BioNematon_p `pvalue3')


xml_tab tableC3_Nem* , save($output/Table_C3.xls) sheet(Nematode) sd2 replace stats(N Umet_dpdx Umet_se Umet_p Orizon_dpdx Orizon_se Orizon_p BioNematon_dpdx BioNematon_se BioNematon_p) format(SCLR3 NCLR3) keep(price Tp) title(TRIMMED SAMPLE - Table C3 Nematode Results) 



///// BOLLWORMS //////

use "${temp}/_tableThreeBw.dta", clear

// BASELINE ESTIMATION
quietly asclogit choice1 price Tp if block!=2 & block!=15 & round==0 & out!=1, case(id) alt(alternative) casevars(TREAT) basealternative(3) vce(cluster EA) 
estat mfx, varlist(TREAT)
eststo margins
foreach x in 1 2 3 4 5 6 {
	matrix mat_`x' = r(_`x')
	local dpdx`x' = mat_`x'[1,1]
	local se`x' = mat_`x'[1,2]
	local pvalue`x' = mat_`x'[1,4]
}

quietly asclogit choice1 price Tp if block!=2 & block!=15 & round==0 & out!=1, case(id) alt(alternative) casevars(TREAT) basealternative(3) vce(cluster EA) 
eststo tableC3_Bw_base, addscalars(Phoskill_dpdx `dpdx1' Phoskill_se `se1' Phoskill_p `pvalue1' Benefit_dpdx `dpdx2' Benefit_se `se2' Benefit_p `pvalue2' ///
	Profenofos_dpdx `dpdx3' Profenofos_se `se3' Profenofos_p `pvalue3' Bollpack_dpdx `dpdx4' Bollpack_se `se4' Bollpack_p `pvalue4' ///
	Blast_dpdx `dpdx5' Blast_se `se5' Blast_p `pvalue5' AlphaGold_dpdx `dpdx6' AlphaGold_se `se6' AlphaGold_p `pvalue6')

 
	
// ENDLINE ESTIMATION
 asclogit choice1 price Tp if block!=2 & block!=15 & round==1 & out!=1, case(id) alt(alternative) casevars(TREAT) basealternative(3) vce(cluster EA) 
 estat mfx, varlist(TREAT)
eststo margins_end
foreach x in 1 2 3 4 5 6 {
	matrix mat_`x' = r(_`x')
	local dpdx`x' = mat_`x'[1,1]
	local se`x' = mat_`x'[1,2]
	local pvalue`x' = mat_`x'[1,4]
}

quietly asclogit choice1 price Tp if block!=2 & block!=15 & round==1 & out!=1, case(id) alt(alternative) casevars(TREAT) basealternative(3) vce(cluster EA) 
eststo tableC3_Bw_end, addscalars(Phoskill_dpdx `dpdx1' Phoskill_se `se1' Phoskill_p `pvalue1' Benefit_dpdx `dpdx2' Benefit_se `se2' Benefit_p `pvalue2' ///
	Profenofos_dpdx `dpdx3' Profenofos_se `se3' Profenofos_p `pvalue3' Bollpack_dpdx `dpdx4' Bollpack_se `se4' Bollpack_p `pvalue4' ///
	Blast_dpdx `dpdx5' Blast_se `se5' Blast_p `pvalue5' AlphaGold_dpdx `dpdx6' AlphaGold_se `se6' AlphaGold_p `pvalue6')


xml_tab tableC3_Bw* , save($output/Table_C3.xls) sheet(Bollworm) sd2 append stats(N Phoskill_dpdx Phoskill_se Phoskill_p Benefit_dpdx Benefit_se Benefit_p  ///
	Profenofos_dpdx Profenofos_se Profenofos_p Bollpack_dpdx Bollpack_se Bollpack_p  ///
	Blast_dpdx Blast_se Blast_p AlphaGold_dpdx AlphaGold_se AlphaGold_p ) ///
	format(SCLR3 NCLR3) keep(price Tp) title(TRIMMED SAMPLE - Table C3 Bollworm Results) 
			
		
////// //////////////// ////// //////////////// ////// //////////////// ////// //////////////// 

////// //////////////// ////// //////////////// ////// //////////////// ////// //////////////// 


///////  TABLE 4 ROBUSTNESS CHECK - TRIMMED SAMPLE
use "${temp}/_tableFour.dta",clear
joinby FMR using "${temp}\trim_BDM.dta", unmatched(both) _merge(_16)
gen trimsample = (TRIM!=1 & replace!=1 & trim_BDMm !=1 & trim_BDMg!=1 & block!=2 & block!=15)

foreach outcome in _insect _insect_Ib _insect_II _insect_U bw bw_Ib bw_II bw_U nem nem_Ib nem_II nem_U {

	// random inference tests
	ritest TREAT _b[TREAT] , cluster(EA) reps(1000) seed(111) :  areg `outcome' TREAT if trimsample==1,  cluster(EA) a(block)	
	matrix `outcome'_rit = r(p)
	local ritest_p = `outcome'_rit[1,1]

	// means
	areg `outcome' TREAT if trimsample==1 & TREAT == 0,  cluster(EA) a(block)	 
	matrix `outcome'_mean = e(b)
	local means_all = `outcome'_mean[1,2]
	
	// econometric output
	areg `outcome' TREAT if trimsample==1,  cluster(EA) a(block)
	testparm TREAT
	local pvalue_all=r(p)
	boottest TREAT, nograph
	local pvalue_wcb = r(p)
	
	eststo tableC4_`outcome'_reg1, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')	
}

// Exporting the regression results
xml_tab tableC4_* , save($output/Table_C4.xls) sd2 replace stats(N r2 pvalue_all pvalue_wcb ritest_p means_all) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(TRIMMED SAMPLE - Table C4 Regression Results) 


////// 


///////  TABLE 5 ROBUSTNESS CHECK - TRIMMED SAMPLE
// BASELINE
use "${temp}/_tableFiveRd1.dta", clear
joinby FMR using "${temp}\trim_BDM.dta", unmatched(both) _merge(_16)
gen trimsample = (TRIM!=1 & replace!=1 & trim_BDMm !=1 & trim_BDMg!=1 & block!=2 & block!=15)
keep if trimsample==1

 
foreach pest in bw nem {
	gen `pest'_high = `pest'==1 & who==1
	gen `pest'_mid = `pest'==1 & (who==2|who==3)
	gen `pest'_low = `pest'==1 & who==4
	}

collapse (sum)bw_* nem_* (first) chiSq*, by(TREAT)

foreach pest in bw nem {
	egen `pest'_tot = rowtotal(`pest'_*)
	foreach tox in high mid low {
		gen `pest'_`tox'_share = `pest'_`tox'/`pest'_tot
		}
	}
keep TREAT bw_tot bw_high_share-nem_low_share chi*
order chi*, last
xpose, clear

mkmat v1 v2, matrix(baseline)

putexcel set "$output/Table_C5.xlsx", sheet("baseline") replace
putexcel A1=("Baseline purchases") A2=("Treatment status") A3=("N bw") A4 = ("BW-High Share") A5=("BW-Mid Share") A6=("BW-Low Share") ///
	A7=("N nem") A8=("Nem-High Share") A9=("Nem-Mid Share") A10=("Nem-Low share") A11=("BW Chi-square") A12=("BW Chi-square p-value")
putexcel B2=matrix(baseline), nformat(number_d2)

// ENDLINE
use "${temp}/_tableFiveRd2.dta", clear
joinby FMR using "${temp}\trim_BDM.dta", unmatched(both) _merge(_16)
gen trimsample = (TRIM!=1 & replace!=1 & trim_BDMm !=1 & trim_BDMg!=1 & block!=2 & block!=15)
keep if trimsample==1

foreach pest in bw nem {
	gen `pest'_high = `pest'==1 & who==1
	gen `pest'_mid = `pest'==1 & (who==2|who==3)
	gen `pest'_low = `pest'==1 & who==4
	}

collapse (sum)bw_* nem_* (first) chiSq*, by(TREAT)

foreach pest in bw nem {
	egen `pest'_tot = rowtotal(`pest'_*)
	foreach tox in high mid low {
		gen `pest'_`tox'_share = `pest'_`tox'/`pest'_tot
		}
	}
keep TREAT bw_tot bw_high_share-nem_low_share chi*
order chi*, last
xpose, clear

mkmat v1 v2, matrix(endline)

putexcel set "$output/Table_C5.xlsx", sheet("endline") modify
putexcel A1=("Endline purchases") A2=("Treatment status") A3=("N bw") A4 = ("BW-High Share") A5=("BW-Mid Share") A6=("BW-Low Share") ///
	A7=("N nem") A8=("Nem-High Share") A9=("Nem-Mid Share") A10=("Nem-Low share") A11=("BW Chi-square") A12=("BW Chi-square p-value") ///
	A13=("Nem Chi-square") A14=("Nem Chi-square p-value")
putexcel B2=matrix(endline), nformat(number_d2)



///////  TABLE 6 ROBUSTNESS CHECK - TRIMMED SAMPLE
use "${temp}/_tableSix.dta", clear

forvalues x = 0(1)1 {
	// random inference tests
	ritest TREAT _b[TREAT] , cluster(EA) reps(1000) seed(111) :  areg bid TREAT if mask==`x' & trimsample==1,  cluster(EA) a(block)	
	matrix bid_rit = r(p)
	local ritest_p = bid_rit[1,1]

	// means
	areg bid TREAT if mask==`x' & trimsample==1 & TREAT==0,  cluster(EA) a(block)	 
	matrix bid_mean = e(b)
	local means_all = bid_mean[1,2]
	
	// econometric output
	areg bid TREAT if mask==`x' & trimsample==1,  cluster(EA) a(block)
	testparm TREAT
	local pvalue_all=r(p)
	boottest TREAT, nograph
	local pvalue_wcb = r(p)
	
	eststo tableC6_MaskEqual_`x'_reg1, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')	
	
	// with covariate controls
		// random inference tests
	ritest TREAT _b[TREAT] , cluster(EA) reps(1000) seed(111) :  areg bid TREAT Bus read HORT_all if mask==`x' & trimsample==1,  cluster(EA) a(block)	
	matrix bid_rit = r(p)
	local ritest_p = bid_rit[1,1]

	// means
	areg bid TREAT if mask==`x' & trimsample==1 & TREAT==0,  cluster(EA) a(block)	 
	matrix bid_mean = e(b)
	local means_all = bid_mean[1,2]
	
	// econometric output
	areg bid TREAT Bus read HORT_all if mask==`x' & trimsample==1,  cluster(EA) a(block)
	testparm TREAT
	local pvalue_all=r(p)
	boottest TREAT, nograph
	local pvalue_wcb = r(p)
	
	eststo tableC6_MaskEqual_`x'_reg2, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_all `pvalue_all' pvalue_wcb `pvalue_wcb')	
	
}

// Exporting the regression results
xml_tab tableC6_* , save($output/Table_C6.xls) sd2 replace stats(N r2 pvalue_all pvalue_wcb ritest_p means_all) ///
	format(SCLR3 NCLR3) keep(TREATMENT) title(TRIMMED SAMPLE - Table C6 Regression Results) 

	
	
///////  TABLE 7 ROBUSTNESS CHECK - TRIMMED SAMPLE
use "${temp}/_tableSix.dta", clear

/////  ROBUSTNESS CHECKS
//////// TRIMMED SAMPLE


forvalues x = 0(1)1 {
	foreach cov in Ib U {
		// random inference tests
		
		ritest TREAT _b[1.TREAT#c.`cov'] , cluster(EA) reps(1000) seed(111) :  areg bid i.TREAT##c.`cov' if mask==`x' & trimsample==1,  cluster(EA) a(block)	
		matrix _rit = r(p)
		local ritest_p = _rit[1,1]

		// means
		sum `cov' if mask==`x' & trimsample==1 & TREAT==0	 
		local means_all = r(mean)
	
		// econometric output
		areg bid i.TREAT##c.`cov' if mask==`x' & trimsample==1,  cluster(EA) a(block)
		testparm 1.TREAT
		local pvalue_treat=r(p)
		testparm c.`cov'
		local pvalue_cov=r(p)
		testparm 1.TREAT#c.`cov'
		local pvalue_Int=r(p)
		
		
		boottest 1.TREAT, nograph
		local pvalue_wcb_treat = r(p)
		boottest c.`cov', nograph
		local pvalue_wcb_cov = r(p)
		boottest 1.TREAT#c.`cov', nograph
		local pvalue_wcb_Int = r(p)
		
		
	eststo tableC7_`cov'_mask`x'_1, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_treat `pvalue_treat' ///
		pvalue_cov `pvalue_cov' pvalue_Int `pvalue_Int' pvalue_wcb_treat `pvalue_wcb_treat' pvalue_wcb_cov `pvalue_wcb_cov' ///
		pvalue_wcbInt `pvalue_wcb_Int')	
	
			// with interaction terms
		ritest TREAT _b[1.TREAT#c.`cov'] , cluster(EA) reps(1000) seed(111) :  areg bid i.TREAT##c.`cov' Bus HORT_all read if mask==`x' & trimsample==1,  cluster(EA) a(block)	
		matrix _rit = r(p)
		local ritest_p = _rit[1,1]

		// means
		sum `cov' if mask==`x' & trimsample==1 & TREAT==0	 
		local means_all = r(mean)
	
		// econometric output
		areg bid i.TREAT##c.`cov' Bus HORT_all read if mask==`x' & trimsample==1,  cluster(EA) a(block)
		testparm 1.TREAT
		local pvalue_treat=r(p)
		testparm c.`cov'
		local pvalue_cov=r(p)
		testparm 1.TREAT#c.`cov'
		local pvalue_Int=r(p)
		
		
		boottest 1.TREAT, nograph
		local pvalue_wcb_treat = r(p)
		boottest c.`cov', nograph
		local pvalue_wcb_cov = r(p)
		boottest 1.TREAT#c.`cov', nograph
		local pvalue_wcb_Int = r(p)
		
		
	eststo tableC7_`cov'_mask`x'_2, addscalars(ritest_p `ritest_p' means_all `means_all' pvalue_treat `pvalue_treat' ///
		pvalue_cov `pvalue_cov' pvalue_Int `pvalue_Int' pvalue_wcb_treat `pvalue_wcb_treat' pvalue_wcb_cov `pvalue_wcb_cov' ///
		pvalue_wcbInt `pvalue_wcb_Int')	
		
		
		}
	}


// Exporting the regression results
xml_tab tableC7_* , save($output/Table_C7.xls) sd2 replace stats(N r2 pvalue_treat pvalue_cov pvalue_Int pvalue_wcb_treat ///
	pvalue_wcb_cov	pvalue_wcbInt ritest_p means_all)format(SCLR3 NCLR3) ///
	keep(1.TREATMENT Ib 1.TREATMENT#c.Ib U 1.TREATMENT#c.U) title(TRIMMED SAMPLE - Table C7 Regression Results) 


timer off 1
timer list 1

